2 回答
TA贡献1719条经验 获得超6个赞
在 GORM 中,默认外键使用所有者的类型名称加上其主键。GORM 提供了一种自定义外键的方法,例如:
type Employee struct {
EmployeeID int64 `gorm:"primary_key;column:employee_id"`
EmployeeCode string `gorm:"column:employee_code"`
FirstName string `gorm:"column:first_name"`
LastName string `gorm:"column:last_name"`
DesignationID int64 `gorm:"column:designation_id;"`
Designation *Designation `gorm:"foreignkey:DesignationID"`
}
TA贡献1827条经验 获得超7个赞
只需将变量名称从 DesignationID 更改为 DesignationDesignationID。它会起作用的。根据文档 ForignKey 必须是 TABLE_NAME + PRIMARY_KEY_NAME 所以这里的表是 DESIGNATION 和主键 DESIGNATIONID
type Employee struct {
EmployeeID int64 `gorm:"primary_key;column:employee_id"`
EmployeeCode string `gorm:"column:employee_code"`
FirstName string `gorm:"column:first_name"`
LastName string `gorm:"column:last_name"`
DesignationDesignationID int64
Designation *Designation
}
type Designation struct {
DesignationID int64 `gorm:"primary_key;`
DesignationName string `gorm:"column:designation_name"`
}
- 2 回答
- 0 关注
- 184 浏览
添加回答
举报