GORM Playground Link
具体问题见下
Description
// 短信模板
type SMSTemplate struct {
gorm.Model
Name string // 模板名称,必填
Description string // 模板描述,可选
TemplateID string // 模板ID,必填
Type string // 模板类型,必填
Vars map[string]bool `gorm:"serializer:json"` // 模板变量名及必须性
}
// 短信关联记录
type SMSRecord struct {
gorm.Model
TemplateID int
Template SMSTemplate `gorm:"foreignKey:TemplateID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` // 关联模板
APPID string // 关联应用ID,必填
Name string // 记录描述,必填
}
- 当按上面模型声明时,会生成不正确的table sql语句。
- 当
SMSRecord.Template移除foreignKey:TemplateID保留references:ID时,生成table sql符合预期 - 当
SMSRecord.Template同时移除foreignKey:TemplateID和references:ID时,生成table sql符合预期
驱动版本
gorm.io/driver/postgres v1.3.5
gorm.io/gorm v1.23.5
结论
当gorm model同时指定foreignKey和references时如果关联模型同时存在foreignKey相同字段时,不能正确生成table sql语句。
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨