有兩張表
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")