doc: add documentation for schema-type option (#487)

This commit is contained in:
Ariel Mashraki
2020-05-11 19:55:10 +03:00
committed by GitHub
parent cfee55e514
commit daf658e265

View File

@@ -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.