Node.js Sequelize:使用外键迁移时出错
Node.js Sequelize: Error in migration with foreign key
我有一个现有的 SQL 服务器数据库,我必须在其中创建一个新的 table。
现有 图片 table 有如图所示的列:
我正在创建一个新的 table 使用带有引用此图像的列的迁移 table。
我的查询是
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('NewTable', {
IdNewTable: {
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false
},
IdImage: {
type: Sequelize.UUID,
references: {
model: 'Image',
key: 'IdImage',
},
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('NewTable');
}
};
在 运行 迁移时,出现此错误
ERROR: Could not create constraint or index. See previous errors.
有人可以帮我解决这个问题吗?
我使用了错误的数据类型
应该是
type: 'UNIQUEIDENTIFIER',
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
allowNull: false,
我有一个现有的 SQL 服务器数据库,我必须在其中创建一个新的 table。
现有 图片 table 有如图所示的列:
我正在创建一个新的 table 使用带有引用此图像的列的迁移 table。
我的查询是
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('NewTable', {
IdNewTable: {
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false
},
IdImage: {
type: Sequelize.UUID,
references: {
model: 'Image',
key: 'IdImage',
},
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('NewTable');
}
};
在 运行 迁移时,出现此错误
ERROR: Could not create constraint or index. See previous errors.
有人可以帮我解决这个问题吗?
我使用了错误的数据类型
应该是
type: 'UNIQUEIDENTIFIER',
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
allowNull: false,