在使用Sequelize进行迁移时遇到问题。这些迁移是由Sequelize-cli创建的,它在迁移名称之前添加了一个日期。 例如:对于Card型号,我得到了20191112123726-create-card.js
问题是我的Card模型有3个外键,当我进行迁移时,文件以“字母顺序”运行,所以我得到了
ERR : Cannot add foreign key constraint
因为我的其他模型的迁移文件中有2个是我的Card模型中的外键,所以会在我的Card迁移文件之后调用...
这有一个好的做法吗?我应该将所有迁移代码都放在同一个文件中吗?这是我的卡迁移文件:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Cards', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
associatedCards: {
type: Sequelize.STRING
},
gameRelativePath: {
type: Sequelize.STRING
},
fullRelativePath: {
type: Sequelize.STRING
},
region: {
type: Sequelize.STRING
},
regionRef: {
type: Sequelize.STRING
},
regionId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Regions',
key: 'id',
}
},
attack: {
type: Sequelize.INTEGER,
},
cost: {
type: Sequelize.INTEGER,
},
health: {
type: Sequelize.INTEGER,
},
description: {
type: Sequelize.STRING,
},
descriptionRaw: {
type: Sequelize.STRING,
},
flavorText: {
type: Sequelize.STRING,
},
artistName: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
cardCode: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
keywords: {
type: Sequelize.STRING,
},
keywordRefs: {
type: Sequelize.STRING,
},
spellSpeed: {
type: Sequelize.STRING,
},
spellSpeedRef: {
type: Sequelize.STRING,
},
spellSpeedId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'SpellSpeeds',
key: 'id',
}
},
rarity: {
type: Sequelize.STRING,
},
rarityRef: {
type: Sequelize.STRING,
},
rarityId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Rarities',
key: 'id',
}
},
subtype: {
type: Sequelize.STRING,
},
supertype: {
type: Sequelize.STRING,
},
type: {
type: Sequelize.STRING,
},
collectible: {
type: Sequelize.BOOLEAN,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Cards');
}
};
在我的情况下,Rarity迁移文件和SpellSpeed迁移文件被调用了,这一切都搞砸了...
谢谢