From 0a8956865745c35f321071567da8750eb9396c4b Mon Sep 17 00:00:00 2001 From: Hiveer Date: Wed, 25 Sep 2024 16:12:27 +0800 Subject: [PATCH] Set default timestamps before insert --- internal/models/common.go | 3 +++ .../migrations/20240902082008_create_admin_photo.go | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/models/common.go b/internal/models/common.go index 8a97d4552..4d3925fee 100644 --- a/internal/models/common.go +++ b/internal/models/common.go @@ -28,6 +28,9 @@ type times struct { func (t *times) BeforeAppendModel(ctx context.Context, query schema.Query) error { switch query.(type) { + case *bun.InsertQuery: + t.CreatedAt = time.Now() + t.UpdatedAt = time.Now() case *bun.UpdateQuery: //q := query.(*bun.UpdateQuery) //m := q.GetModel().Value() diff --git a/pkg/database/migrations/20240902082008_create_admin_photo.go b/pkg/database/migrations/20240902082008_create_admin_photo.go index 551b144b6..8c1fd2498 100644 --- a/pkg/database/migrations/20240902082008_create_admin_photo.go +++ b/pkg/database/migrations/20240902082008_create_admin_photo.go @@ -8,9 +8,9 @@ import ( type User struct { ID int64 `bun:",pk,autoincrement" json:"id"` - Nickname string `bun:",notnull" json:"nickname"` - Name string `bun:",notnull,unique" json:"name"` - Email string `bun:",notnull,unique" json:"email"` + Nickname string `bun:"," json:"nickname"` + Name string `bun:"," json:"name"` + Email string `bun:"," json:"email"` LoginIdentity string `bun:",pk,notnull" json:"login_identity"` Gender string `bun:"," json:"gender"` RolesMask int `bun:"," json:"roles_mask"`