javascript - Sequelize 迁移给出“错误:无法添加外键约束”
问题描述
我正在尝试使用 Sequelize 迁移、组织和组织类型创建两个表。但是,当我进行迁移以创建 Organizations 表时,我得到一个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'
}
});
}
解决方案
配置似乎自相矛盾:
// ...
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
推荐阅读
- java - 使用 Matlab Java API 依赖构建 Spring-Boot Maven 项目
- node.js - 检查 mongodb 文档是否具有未在模式中定义的属性?
- javascript - 从函数返回的文本中删除子字符串
- apple-watch - 如何从 Apple Watch 上的高度计传感器访问原始数据?
- symfony - Symfony中具有属性长度的约束数字类型?
- node.js - Dialogflow - 使用异步/等待从数据库中读取
- java - 如何为 try-resource 和等效的突变体重构幸存的突变体?
- spring-security - 如何使用数据库中的数据丰富 Oauth2 身份验证?
- javascript - 如何处理 vue js 模板中不存在的变量?
- reactjs - 如何将 react-bootstrap 轮播与 gatsby-image 一起使用?