How do I migrate sequelize with 2 table referencing each other? for example i have table roles & users, in table roles i have column created_by referencing to table users and in table users i have column roles_id referencing to roles, when i tried to run normally it says "relation roles does not exist"
Here is my code:
"use strict"
module.exports = {
up: async (queryInterface, Sequelize) => {
// roles
await queryInterface.createTable("roles", {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
},
created_by: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: "users",
key: "id",
},
onDelete: "CASCADE",
onUpdate: "CASCADE",
},
})
await queryInterface.addIndex("roles", {
fields: ["name"],
})
// users
await queryInterface.createTable("users", {
id: {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
roles_id: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: "roles",
key: "id",
onDelete: "CASCADE",
onUpdate: "CASCADE",
},
},
})
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("roles")
await queryInterface.dropTable("users")
},
}