【发布时间】:2017-03-23 19:51:46
【问题描述】:
Sequelize 删除了我的外键中的下划线。
Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });
结果如下:
Unknown column 'UserRole.UserId' in 'field list'
因为该列名为 user_id,而不是 UserId。
即使设置了underscore: true 选项,它也会这样做。
有没有办法解决这个问题,这让我发疯了,因为,我不知道我做错了还是续集。
UserRole.js:
module.exports = {
attributes: {
roleId: {
type: Sequelize.INTEGER(11),
field: 'role_id',
primaryKey: true
},
userId: {
type: Sequelize.INTEGER(11),
field: 'user_id',
primaryKey: true
}
},
associations: function() {
UserRole.belongsTo(User, { foreignKey: 'user_id' });
UserRole.hasOne(Role, { foreignKey: 'id' });
},
options: {
tableName: 'user_roles',
timestamps: false,
classMethods: {},
instanceMethods: {},
hooks: {}
}
}
角色.js
module.exports = {
attributes: {
id: {
type: Sequelize.INTEGER(11),
primaryKey: true
},
name: {
type: Sequelize.STRING(50),
},
description: {
type: Sequelize.STRING(50),
},
},
associations: function() {
Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });
},
options: {
tableName: 'roles',
timestamps: false,
classMethods: {},
instanceMethods: {},
hooks: {}
}
}
【问题讨论】:
-
您确定您已尝试开启
underscore选项吗?我在这里的代码中看不到这一点,这很重要。 -
@tadman 我应该睡觉了,忘记在 User 模型中添加
underscored选项。非常感谢! -
发生在我们最好的人身上。很高兴你得到它!
标签: mysql node.js sequelize.js