我正在尝试使用Sequelize迁移创建两个表Organizations和OrganisationTypes。但是,当我进行迁移以创建“组织”表时,会得到一个ERROR: Cannot add foreign key constraint
。我看不到任何可能出错的东西,例如类型或名称不匹配。
这是两个迁移(1:OrganizationTypes; 2:Organizations):
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('OrganisationTypes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true,
type: Sequelize.INTEGER
},
name: {
allowNull: false,
unique: true,
type: Sequelize.STRING
}
});
}
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Organisations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
unique: true,
type: Sequelize.INTEGER
},
name: {
allowNull: false,
unique: true,
type: Sequelize.STRING(60)
},
organisationTypeId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'OrganisationTypes',
key: 'id'
},
onDelete: 'SET NULL'
}
});
}
答案 0 :(得分:1)
配置似乎自相矛盾:
// ...
organisationTypeId: {
allowNull: false, // <--- here it's not allowed to be null
type: Sequelize.INTEGER,
references: {
model: 'OrganisationTypes',
key: 'id'
},
onDelete: 'SET NULL' // <--- yet here it's supposed to be set null upon parent deletion