mirror of
https://github.com/ent/ent.git
synced 2026-05-02 07:30:53 +03:00
dialect/entsql: add support for column default using annotation
Fixed #1033
This commit is contained in:
committed by
Ariel Mashraki
parent
3db3f5fd1a
commit
745afde770
@@ -290,6 +290,24 @@ func (User) Fields() []ent.Field {
|
||||
}
|
||||
```
|
||||
|
||||
SQL-specific expressions like function calls can be added to default value configuration using the
|
||||
[`entsql.Annotation`](https://pkg.go.dev/entgo.io/ent@master/dialect/entsql#Annotation):
|
||||
|
||||
```go
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// Add a new field with CURRENT_TIMESTAMP
|
||||
// as a default value to all previous rows.
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
Annotations(&entsql.Annotation{
|
||||
Default: "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In case your `DefaultFunc` is also returning an error, it is better to handle it properly using [schema-hooks](hooks.md#schema-hooks).
|
||||
See [this FAQ](faq.md#how-to-use-a-custom-generator-of-ids) for more information.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user