Automatic updated_at with usage of Column() #621
-
Hi, I am using automatic timestamps discribed in the doc and I found when my update query contains // times has now as its default on inserting and updating
type times struct {
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"createdAt"`
UpdatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"updatedAt"`
}
var _ bun.BeforeAppendModelHook = (*times)(nil)
func (t *times) BeforeAppendModel(ctx context.Context, query schema.Query) error {
switch query.(type) {
case *bun.UpdateQuery:
t.UpdatedAt = time.Now()
}
return nil
}
type OAuth2State struct {
ID string `json:"id" bun:",pk"`
times
Mode OAuth2StateMode `json:"mode" bun:",notnull,nullzero"`
Status OAuth2Status `json:"status" bun:",notnull"`
}
func (op Operator) UpdateOAuth2StateStatusByID(ctx context.Context, id string, status OAuth2Status) (err error) {
state := OAuth2State{ID: id, Status: status}
err = assertAffectedOneRow(op.db.NewUpdate().
Model(&state).
Column("status"). // only status appears in the SET section of the generated SQL
WherePK().
Exec(ctx))
return
} Did I miss something? Any idea would be appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
vmihailenco
Aug 21, 2022
Replies: 1 comment 1 reply
-
This is the expected behavior, not a bug. Just include updated_at column to be updated. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nanmu42
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the expected behavior, not a bug. Just include updated_at column to be updated.