简体   繁体   中英

Node.js Sequelize: Error in migration with foreign key

I have an existing SQL Server database where I have to create a new table.

Existing Image table has column as shown in image:

在此处输入图片说明

I'm creating a new table using migration with a column referencing to this Image table.

My Query is

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');
  }
};

While running a migration, I'm getting this error

ERROR: Could not create constraint or index. See previous errors.

Can someone please help me in this?

I was using the wrong datatype

It should be

type: 'UNIQUEIDENTIFIER',
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
allowNull: false,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM