有两张表
type App struct {
Id uint `gorm:"primary_key;type int(11) unsigned auto increment"`
Name string
Desc string
}
type Data stuct {
Id uint `gorm:"primary_key;type int(11) unsigned auto increment"`
Name string
App App `gorm:"ForeignKey:Id;AssociationForeignKey:AppId"`//设置外键
AppId uint `json:"app_id"
}
通过标签的方式设置外键,发现并不生效。
解决方法:
db.Model(&data).AddForeignKey("app_id", "app(id)", "RESTRICT", "RESTRICT")