Why is it that in some of my models, sequelize WON’T create a new column for foreignkey? BUT it does create for other models??? It’s frustrating and weird. For instance, in this User model, sequelize won’t create role_id
.
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
id: { type: DataTypes.BIGINT, allowNull: false, autoIncrement: true, unique: true, primaryKey: true },
first_name: DataTypes.STRING,
last_name: DataTypes.STRING
}, {});
User.associate = function(models) {
User.belongsTo(models.Role, { foreignKey: 'role_id' });
};
return User;
};
This is a similar question: Sequelize not creating model association columns BUT! It wasn't answered.
I've spent hours on this, I did everything like:
- Reading this thoroughly: https://sequelize.org/master/manual/assocs.html
- Experimenting, like creating a new dummy model, with name
NewUser
. It works! But again not with User
name.
- Posted on Sequelize's Slack channel.
After this Stackoverflow question, I will seek help from their Github's issue page.
I'm thinking I can just define the column role_id
instead of adding it through the associate
function.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…