解决方法:
type User struct {
gorm.Model
Name string
SchoolId int
School School `gorm:"foreignKey:SchoolId"` // 使用SchoolId作为外键
}
type School struct {
ID int
Class string
}
如果还报错修改为:
type User struct {
gorm.Model
Name string
School School `gorm:"association_foreignkey:ID"` // School的ID作为关联外键
}
type School struct {
ID int
Class string
}
这样就不会报错了!