Sequelize CLI:迁移数据库时无法读取未定义的 属性 'replace'
Sequelize CLI : cannot read property 'replace' of undefined when migrating DB
我正在尝试使用 db:migrate
函数迁移数据库,但是我收到错误消息:
ERROR: Cannot read property 'replace' of undefined
我已在 GitHub 上针对此错误参考了其他解决方案,但 none 解决了该问题。我正在使用 sequelize-cli 执行迁移。
这是我的模型:
'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');
}
};
我试图通过删除它来执行其他模型,但似乎没有任何效果。请帮帮我!
更新
我发现这是由于我试图添加的关系引起的,所以我在一个单独的迁移中移动了所有帖子的所有关系:
'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) => {
}
};
现在,所有表都已创建,但是此迁移的错误仍然存在。
我遇到了同样的错误。
解决方法:我改
...
references: TABLE_NAME,
referenceKey: COLUMN_NAME
...
到
...
references: {
model: TABLE_NAME,
key: COLUMN_NAME
...
有效
对我来说,这是因为我的环境变量在执行 npx sequelize-cli db:create
时没有被加载。当我设法加载它时(在我的 config.js 文件中加载 dot-env + dot-env cli 可以肯定)它工作正常。
如果你使用IDE,你可以爬到未定义的定义。或者只检查松弛轨迹。
对我来说,就是那一行:
at getCreateDatabaseQuery (D:\Code\restaurant_project\website_engine\back_end\node_modules\sequelize-cli\lib\commands\database.js:82:64)
指向脚本中的那一行(ctrl+单击 slack trace 转到文件)
return 'CREATE DATABASE IF NOT EXISTS ' + queryGenerator.quoteIdentifier(config.database)
config.database
未定义,因此出现错误。
我正在尝试使用 db:migrate
函数迁移数据库,但是我收到错误消息:
ERROR: Cannot read property 'replace' of undefined
我已在 GitHub 上针对此错误参考了其他解决方案,但 none 解决了该问题。我正在使用 sequelize-cli 执行迁移。
这是我的模型:
'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');
}
};
我试图通过删除它来执行其他模型,但似乎没有任何效果。请帮帮我!
更新
我发现这是由于我试图添加的关系引起的,所以我在一个单独的迁移中移动了所有帖子的所有关系:
'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) => {
}
};
现在,所有表都已创建,但是此迁移的错误仍然存在。
我遇到了同样的错误。
解决方法:我改
...
references: TABLE_NAME,
referenceKey: COLUMN_NAME
...
到
...
references: {
model: TABLE_NAME,
key: COLUMN_NAME
...
有效
对我来说,这是因为我的环境变量在执行 npx sequelize-cli db:create
时没有被加载。当我设法加载它时(在我的 config.js 文件中加载 dot-env + dot-env cli 可以肯定)它工作正常。
如果你使用IDE,你可以爬到未定义的定义。或者只检查松弛轨迹。
对我来说,就是那一行:
at getCreateDatabaseQuery (D:\Code\restaurant_project\website_engine\back_end\node_modules\sequelize-cli\lib\commands\database.js:82:64)
指向脚本中的那一行(ctrl+单击 slack trace 转到文件)
return 'CREATE DATABASE IF NOT EXISTS ' + queryGenerator.quoteIdentifier(config.database)
config.database
未定义,因此出现错误。