Gorm 不遵守 many2many 说明符
Gorm not adhering to many2many specifier
我在使用 Go 的 Gorm 时遇到问题。
我有一个名为 scopes 的 table 和一个名为 employees 的 table。
我还有一个 many2many/intermediary table 叫 employee_scopes.
当我试图用指定的范围保存员工时。我收到一个错误消息,Gorm 正试图将记录插入到中介 table 中,就好像它是作用域 table.
错误信息:
错误 1054:'field list'
中的未知列 'scope'
型号:
type Employee struct {
gorm.Model
FirstName string `json:"FirstName" gorm:"column:first_name;type:varchar(100);not null"`
Scopes []EmployeeScope `json:"Scopes" gorm:"many2many:employee_scopes"`
}
type EmployeeScope struct {
gorm.Model
Scope string `json:"Scope" `
}
SQL:
CREATE TABLE `employees`
(
ID INT(6) AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
);
CREATE TABLE `scopes`
(
id INT(6) AUTO_INCREMENT,
scope VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE `employee_scopes`
(
id INT(6) AUTO_INCREMENT,
employee_id INT(6) NOT NULL,
scope_id INT(6) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (employee_id) REFERENCES employees (id),
FOREIGN KEY (scope_id) REFERENCES scopes (id)
);
Gorm 好像没有听我指定的内容。我尝试使用正确的说明符添加反向引用,但我得到了同样的错误。我还添加了 many 2 many overrides,但这也没有用。
Error Message: Error 1054: Unknown column 'scope' in 'field list'
因为 gorm 使用 Table 名称作为结构名称 (Ref) 的复数形式。这就是 gorm 尝试将数据插入 employee_scopes
table 的原因。您的 EmployeeScope
结构名称应该是 Scope
.
type Employee struct {
gorm.Model
FirstName string `json:"FirstName" gorm:"column:first_name;type:varchar(100);not null"`
Scopes []Scope `json:"Scopes" gorm:"many2many:employee_scopes"`
}
type Scope struct {
gorm.Model
Scope string `json:"Scope" `
}
我在使用 Go 的 Gorm 时遇到问题。
我有一个名为 scopes 的 table 和一个名为 employees 的 table。 我还有一个 many2many/intermediary table 叫 employee_scopes.
当我试图用指定的范围保存员工时。我收到一个错误消息,Gorm 正试图将记录插入到中介 table 中,就好像它是作用域 table.
错误信息: 错误 1054:'field list'
中的未知列 'scope'型号:
type Employee struct {
gorm.Model
FirstName string `json:"FirstName" gorm:"column:first_name;type:varchar(100);not null"`
Scopes []EmployeeScope `json:"Scopes" gorm:"many2many:employee_scopes"`
}
type EmployeeScope struct {
gorm.Model
Scope string `json:"Scope" `
}
SQL:
CREATE TABLE `employees`
(
ID INT(6) AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
);
CREATE TABLE `scopes`
(
id INT(6) AUTO_INCREMENT,
scope VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE `employee_scopes`
(
id INT(6) AUTO_INCREMENT,
employee_id INT(6) NOT NULL,
scope_id INT(6) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (employee_id) REFERENCES employees (id),
FOREIGN KEY (scope_id) REFERENCES scopes (id)
);
Gorm 好像没有听我指定的内容。我尝试使用正确的说明符添加反向引用,但我得到了同样的错误。我还添加了 many 2 many overrides,但这也没有用。
Error Message: Error 1054: Unknown column 'scope' in 'field list'
因为 gorm 使用 Table 名称作为结构名称 (Ref) 的复数形式。这就是 gorm 尝试将数据插入 employee_scopes
table 的原因。您的 EmployeeScope
结构名称应该是 Scope
.
type Employee struct {
gorm.Model
FirstName string `json:"FirstName" gorm:"column:first_name;type:varchar(100);not null"`
Scopes []Scope `json:"Scopes" gorm:"many2many:employee_scopes"`
}
type Scope struct {
gorm.Model
Scope string `json:"Scope" `
}