无法添加外键约束 - SEQUELIZE
Cannot add foreign key constraint - SEQUELIZE
我认为像这样的错误应该与字段类型(数据类型)不匹配有关。但这已被证明是错误的,因为我已尝试将 table id 和外键的数据类型更改为整数和 uuid,但结果仍然相同。我还想指出 nodejs 是全新的,所以你知道我来自哪里......
migration/xxxxxxx-demo-user.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Users', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
phoneNumber: {
type: Sequelize.STRING
},
country: {
type: Sequelize.STRING
},
PlanId: {
type: Sequelize.UUID,
references: {
model: 'Plans',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
}
};
migrations/xxxxxx-demo-plan.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Plans', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
title: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Plans');
}
};
错误信息
尝试重命名您的“xxxxxxx-demo-user.js”和“xxxxxxx-demo-plan.js”迁移文件名。一开始您在计划迁移之前进行了 运行 用户迁移,这就是为什么您的“planId”字段(用户 table)指向不存在的计划 table。尝试这样的操作,然后 运行 再次迁移:
20210918101111-demo-plan.js
20210918102222-demo-user.js
sequelize db:migrate:undo:all
sequelize db:migrate
我认为像这样的错误应该与字段类型(数据类型)不匹配有关。但这已被证明是错误的,因为我已尝试将 table id 和外键的数据类型更改为整数和 uuid,但结果仍然相同。我还想指出 nodejs 是全新的,所以你知道我来自哪里......
migration/xxxxxxx-demo-user.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Users', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
phoneNumber: {
type: Sequelize.STRING
},
country: {
type: Sequelize.STRING
},
PlanId: {
type: Sequelize.UUID,
references: {
model: 'Plans',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
}
};
migrations/xxxxxx-demo-plan.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Plans', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
title: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Plans');
}
};
错误信息
尝试重命名您的“xxxxxxx-demo-user.js”和“xxxxxxx-demo-plan.js”迁移文件名。一开始您在计划迁移之前进行了 运行 用户迁移,这就是为什么您的“planId”字段(用户 table)指向不存在的计划 table。尝试这样的操作,然后 运行 再次迁移:
20210918101111-demo-plan.js
20210918102222-demo-user.js
sequelize db:migrate:undo:all
sequelize db:migrate