Search code examples
mysqlnode.jsforeign-keysmigrationconstraints

sequelize error: missing index for constraint


20181005120552-create-order-detail.js

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('OrderDetails', {
      orderDetailId: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        allowNull: false,
        autoIncrement: true,
      },
      orderId: {
        type: Sequelize.INTEGER,
        onDelete: 'CASCADE',
        references: {
          model: 'Orders',
          key: 'orderId'
        }
      },
      productName: {
        type: Sequelize.STRING,
        primaryKey: true,
        allowNull: false,
      },
      count: {
        type: Sequelize.INTEGER
      },
      orderDetailPrice: {
        type: Sequelize.INTEGER,
        onDelete: 'CASCADE',
        references: {
          model: 'Orders',
          key: 'totalPrice'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('OrderDetails');
  }
};

20181005120522-create-order

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface
    .createTable('Orders', {
      orderId: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      userId: {
        type: Sequelize.STRING,
        onDelete: 'CASCADE',
        references: {
          model: 'Users',
          key: 'userId'
        }
      },
      orderDate: {
        type: Sequelize.DATE
      },
      totalPrice: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        allowNull: false,
      },
      orderState: {
        type: Sequelize.STRING
      },
      shippingNumber: {
        type: Sequelize.STRING
      },
      basicAddress: {
        type: Sequelize.STRING
      },
      detailAddress: {
        type: Sequelize.STRING
      },
      telNumber: {
        type: Sequelize.INTEGER
      },
      phoneNumber: {
        type: Sequelize.INTEGER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Orders');
  }
};

When i executed script sequelize db:migrate, previous migration is without errors executed. in this level it returns error. I don't know how can i resolve this problem i guess it has something wrong.

ERROR: Failed to add the foreign key constraint. Missing index for constraint 'orderdetails_ibfk_2' in the referenced table 'orders'

This is error message. I wanna connect files OrderDetails.orderDetailPrice and Orders.totalPrice.

Thanks.


Solution

  • Both keys are written.

    create-order-detail.js

    'use strict';
    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('OrderDetails', {
              (...),
              productName: {
                type: Sequelize.STRING,
                primaryKey: true,
                // allowNull: false
                unique: true
              },
              count: {
                type: Sequelize.INTEGER
              },
              orderDetailPrice: {
                type: Sequelize.INTEGER,
                onDelete: 'CASCADE',
                // references: {
                //     model: 'Orders',
                //     key: 'totalPrice'
                // }
              },
              createdAt: {
                allowNull: false,
                type: Sequelize.DATE
              },
              updatedAt: {
                allowNull: false,
                type: Sequelize.DATE
              }
            })
          .then(() => {
            queryInterface.addConstraint('OrderDetails', ['orderDetailPrice'], {
              type: 'foreign key',
              references: {
                name: 'orderdetails_ibfk_2',
                table: 'Orders',
                field: 'totalPrice'
              },
            })
          })
      },
      down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('OrderDetails');
      }
    };