我是使用 sequelize 和 nodejs 的新手。在以前的 Web 框架中,我已经构建了自己的迁移脚本。现在我想学习使用 sequelize 迁移脚本。我正在寻找有关如何实现以下目标的示例代码或伪代码。我将编写我通常在 PHP 和 MySQL 中执行的操作:
if(!columnExists('tblUser','main_profile_id'))
{
$sql = "ALTER TABLE tblUser ADD COLUMN main_profile_id INT(11) UNSIGNED NOT NULL";
db_execute($sql);
$sql = "UPDATE tblUser SET main_profile_id = (SELECT profile_id FROM tblProfile WHERE tblProfile.user_id = tblUser.user_id AND tblProfile.priority = 0)";
db_execute($sql);
$sql = "ALTER TABLE tblUser ADD FOREIGN KEY (`main_profile_id`) REFERENCES `tblProfile`(`profile_id`)";
db_execute($sql);
}
我运行每个 sql 语句的顺序很重要,因为我需要确保在应用外键约束之前填充了
tblUser.main_profile_id
,否则我会得到一个错误。这个 nodejs + sequelize 翻译是什么?
编辑:如果这对我有帮助,我也在使用 Express
最佳答案
这是编写迁移脚本的示例
"use strict";
module.exports = {
up: function(migration, DataTypes, done) {
migration.addColumn('Locations', 'PostId',
{
type: DataTypes.INTEGER,
references: "Posts",
referencesKey: "id"
});
done();
},
down: function(migration, DataTypes, done) {
migration.removeColumn('Locations', 'PostId');
done();
}
};
关于node.js - 在添加外键约束之前填充表列的迁移脚本示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171321/