gorm設置外鍵不生效問題

有兩張表

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")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章