我有以下 struct
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"`
}
type Designation struct {
DesignationID int64 `gorm:"primary_key;column:designation_id"`
DesignationName string `gorm:"column:designation_name"`
}
func GetEmployee(id int64) (*Employee, error) {
db := connection.GetConn() //get connection
defer db.Close()
employee := &Employee{}
err := db.Model(employees).Preload("Designation", func(db *gorm.DB) *gorm.DB {
return db.Join("INNER JOIN employees ON employees.designation_id = designations.id").Order("employees.first_name DESC")
}).Find(employee).Error
return employee, err
}
生成的查询是
SELECT * FROM employees;
SELECT * FROM designations INNER JOIN employees ON employees.designation_id = designations.id WHERE id IN (1,2) order by employees.first_name DESC;
我有一个类似于我在上面的函数中编写的联接.在这里,我不能只过滤连接,但在实际情况中,我知道连接是必需的,并且预加载表和连接表中都有id
个字段.
由于两个表具有相同的列名,即id
,在这种情况下,MySQL抛出错误
Error 1052: Column 'id' in where clause is ambiguous
个
我将在gorm v1.9.16年内面对这个问题
我没有找到任何提到类似问题的资源. 我们如何解决这个问题?