下划线被删除
问题描述:
Sequelize删除我的外键中的下划线。下划线被删除
Role.belongsToMany(User, { foreignKey: 'user_id', through: UserRole });
结果在此:
Unknown column 'UserRole.UserId' in 'field list'
因为列被命名为USER_ID,而不是用户ID。
即使设置了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: {}
}
}
Role.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
选项。如果设置Sequelize尊重您的命名约定首选项并将按预期链接事物。
我更喜欢下划线的名字,长时间的Rails开发者,以及混合大小写的列名称,这些名称的结果看起来很糟糕。
你确定你已经尝试打开'underscore'选项吗?我在这里看不到你的代码,这很重要。 – tadman
@tadman我应该睡觉了,忘了将'underscored'选项添加到用户模型中。非常感谢! – Joery
发生在我们身上。很高兴你得到它! – tadman