Sequelize CLI : cannot read property 'replace' of undefined when migrating DB
I had this same error.
Solution: I change
...
references: TABLE_NAME,
referenceKey: COLUMN_NAME
...
to
...
references: {
model: TABLE_NAME,
key: COLUMN_NAME
...
and it works
Related videos on Youtube
sameer manek
I am a student who is fascinated by the marvellous things programming can do. I code in php, java and C#. I also make some websites for pocket money, and work on complex private projects including a LMS and MLM(network marketing). I try new frameworks and languages and thus ask a lot many questions. I am 20 yet but have coded 12 web applications, 3 websites, 4 desktop and 1 mobile application single handedly. a lot more in group of 6 people I have. I also like graphics and learning it when get time.
Updated on June 04, 2022Comments
-
sameer manek almost 2 years
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.
-
Dmitry Shvetsov over 3 yearsAnd do not confuse
references.model
withreferences.table
fromqueryInterface.addConstraint
. table option doesn't work withqueryInterface.createTable
andqueryInterface.addColumn
in Sequelize 6