mirror of
https://github.com/ent/ent.git
synced 2026-04-30 06:30:55 +03:00
doc: add documentation for schema-type option (#487)
This commit is contained in:
@@ -51,7 +51,7 @@ The following types are currently supported by the framework:
|
||||
- `string`
|
||||
- `time.Time`
|
||||
- `[]byte` (only supported by SQL dialects).
|
||||
- `JSON` (only supported by SQL dialects) - **experimental**.
|
||||
- `JSON` (only supported by SQL dialects).
|
||||
- `Enum` (only supported by SQL dialects).
|
||||
|
||||
<br/>
|
||||
@@ -139,9 +139,41 @@ func (Pet) Fields() []ent.Field {
|
||||
}
|
||||
```
|
||||
|
||||
## Database Type
|
||||
|
||||
Each database dialect has its own mapping from Go type to database type. For example,
|
||||
the MySQL dialect creates `float64` fields as `double` columns in the database. However,
|
||||
there is an option to override the default behavior using the `SchemaType` method.
|
||||
|
||||
```go
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/facebookincubator/ent"
|
||||
"github.com/facebookincubator/ent/dialect"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
)
|
||||
|
||||
// Card schema.
|
||||
type Card struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Card.
|
||||
func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Float64("amount").
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "decimal(6,2)", // Override MySQL.
|
||||
dialect.Postgres: "numeric", // Override Postgres.
|
||||
}),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Default Values
|
||||
|
||||
**Non-unique** fields support default values using the `.Default` and `.UpdateDefault` methods.
|
||||
**Non-unique** fields support default values using the `Default` and `UpdateDefault` methods.
|
||||
|
||||
```go
|
||||
// Fields of the User.
|
||||
|
||||
Reference in New Issue
Block a user