Open listguy opened 3 years ago
Adding sequelize.sync() to models/index.js solved it.
This is an awful way to solve this problem. The running app should not mess with the DB/schema.
A much more correct way to deal with this is to manually create the model(or just a migration file for it) and allow the migration script to do proper migrations.
Sample migration for the though table
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('GroupMembers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
GroupName: {
allowNull: false,
type: Sequelize.STRING
},
UserUsername: {
allowNull: false,
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('GroupMembers');
}
};
What you are doing?
Trying to create a Many to Many associastion between
Team
andPlayer
usingbelongsToMany
.create-player
migration:create-team
migrationPlayer
modelTeam
modelindex.js
config.json
What do you expect to happen?
A
PlayerTeams
table to be created automatically and the Many to Many association to work when querying inindex.js
.What is actually happening?
Getting an error that the junction table does not exist (was not created)
Output, either JSON or SQL (The error message)
Ps.
Adding
sequelize.sync()
tomodels/index.js
solved it.I couldn't find the solution online, I was just lucky trying it out.
But I was wandering - Is there something wrong in my setup? And if not, I think you should add this line to your default configuration of
models/index.js
(when runningnpx sequelize init
).Otherwise people can't use the full power of sequelize associations when bootstrapping a project with the default configurations you provide ☹
Dialect: mysql Database version: 8.0.23 Sequelize CLI version: 6.2.0 Sequelize version: 6.6.2