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 withUsername. - 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.
ZUser.js(model defined asZUser, and I can see theroldeIdcolumn being added by Sequelize. I really now don't know what's happening. I think it's because of the class name defined asUser???? - Glenn Posadasdb.sequelize.sync({ force : true}). I also have tried deleting ALL the tables (But haven't tried re-creating database). I also tried deleting migrations, and re-running db:migrate/ - Glenn Posadas