使用 Gorm 模型和自定义序列

问题描述 投票:0回答:1

我有一个 seq 'user_tfa_info_seq',我想在 Gorm 模型的 'user_tfa_info' 表中使用它。

我使用了以下结构,但不起作用。

type UserTfaInfo struct{
  ID        uint `gorm:"primary_key;type:bigint(20) not null" sql:"nextval('user_tfa_info_seq')"`
}
postgresql go go-gorm
1个回答
0
投票

最近我遇到了类似的问题并设法解决了。 https://gorm.io/docs/models.html#高级

我使用

gorm:"->"
将该字段声明为只读,因此在创建新记录时,其值由数据库的默认值(例如
nextval('user_tfa_info_seq')
)设置。

© www.soinside.com 2019 - 2024. All rights reserved.