Repository navigation
Replies: 1 comment
|
This is the expected behavior — ent only includes For ent-managed timestamps ( If you define them in your schema, ent sets them on the Go struct at creation time rather than relying on the DB default: field.Time("created_at").
Default(time.Now).
Immutable(),
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),With this, ent writes the value itself and the struct is populated without a round-trip. For trigger-generated or external DB-generated values: Ent cannot introspect trigger outputs. The only reliable way is a second query: created, err := client.MyEntity.Create().
SetField(value).
Save(ctx)
// created.TriggerField is still zero value
// Reload to get trigger-updated columns
refreshed, err := client.MyEntity.Get(ctx, created.ID)
// refreshed.TriggerField now has the DB valueExtending Ent doesn't currently support extending the For the specific case of |
Uh oh!
There was an error while loading. Please reload this page.
I have certain columns that are updated automatically by the database... things like
created_atandupdated_at, or values that are updated by database triggers.But it seems that when I use
Create()to create a new entity, those values are still set to their zero values. The only way I've been able to get the database-generated values populated is to make anotherGet()query to refresh the entity data. Is this the expected way to handle this? I see that the SQL generated is usingRETURNING "id", but I can't seem to find a way to returning additional columns.This seems like a pretty common scenario. I feel like I must be missing something, but I can't seem to find any documentation or discussion about this. Is there an annotation I need to add to these fields so they are included in the
RETURNINGclause? Or is this simply not supported?All reactions