我在postgres中有一个表,其UUID字段类型必须唯一,但可以为空
带着一张桌子&;像这样的模型
CREATE TABLE IF NOT EXISTS asdf(
id bigserial primary key,
name varchar(255) NOT NULL,
key uuid unique,
created_at timestamptz,
updated_at timestamptz
);
go模型定义为
type Asdf struct {
ID uint64 `json:"id" gorm:"type:uuid;column:id"`
Name string `json:"name" gorm:"column:name"`
Key uuid.UUID `json:"key" gorm:"column:key"`
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
}
result := db.Connect().Create(asdf.Asdf{ID:123,Name:"This is the name"})
并将以下sql查询打印到终端
INSERT INTO "asdf" ("id","name","key","created_at","updated_at")
VALUES('123','This is the name','00000000-0000-0000-0000-000000000000','2022-04-27 03:41:49.338','2022-04-27 03:41:49.338')
它将模型插入到数据库中,将00000000-0000-0000-0000-000000000000
作为key
值,而不是空值
我还注意到这种情况发生在字符串类型中,它插入了一个空字符串''
,而不是NULL
如何使gorm insert NULL而不是零/空字符串作为值?