I am trying to migrate DB using db:migrate
function, however I am getting the error printed:
ERROR: Cannot read property 'replace' of undefined
I have referred to other solutions for this error on GitHub but none resolve the issue. I am using sequelize-cli to perform migration.
here is my model:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Containers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
userId: {
type: Sequelize.INTEGER,
references: "Users",
refereceKey: "id",
onUpdate: "cascade",
onDelete: "cascade",
},
type: {
type: Sequelize.STRING
},
detail: {
type: Sequelize.INTEGER
},
checkin: {
type: Sequelize.DATE
},
checkout: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Containers');
}
};
I have tried to execute other models by deleting this and nothing seems to work. Please help me here!
UPDATE
I have figured out that this is caused due to relations that I am trying to add, so I moved all the relations for all the posts in one separate migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return [
queryInterface.addColumn("Containers", "userId", {
type: Sequelize.INTEGER,
references: "Users",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Containers", "parentContainer", {
type: Sequelize.INTEGER,
references: "Containers",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Entities", "containerId", {
type: Sequelize.INTEGER,
references: "Containers",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade",
}),
queryInterface.addColumn("Posts", "userId", {
type: Sequelize.INTEGER,
references: "Users",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Posts", "containerId", {
type: Sequelize.INTEGER,
references: "Containers",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade",
nullable: true
}),
queryInterface.addColumn("Votes", "userId", {
type: Sequelize.INTEGER,
references: "Users",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Votes", "postId", {
type: Sequelize.INTEGER,
references: "Posts",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Comments", "userId", {
type: Sequelize.INTEGER,
references: "Users",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Comments", "postId", {
type: Sequelize.INTEGER,
references: "Posts",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
queryInterface.addColumn("Comments", "commentId", {
type: Sequelize.INTEGER,
references: "Comments",
referenceKey: "id",
onUpdate: "cascade",
onDelete: "cascade"
}),
]
},
down: (queryInterface, Sequelize) => {
}
};
Now, all the tables are created BUT, the error persists for this migration.
references.model
withreferences.table
fromqueryInterface.addConstraint
. table option doesn't work withqueryInterface.createTable
andqueryInterface.addColumn
in Sequelize 6 – Aves