ORM: Sequelize: migration실행하면 에러가 납니다

조회수 1037회

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', {
      // id: {
      //   allowNull: false,
      //   autoIncrement: true,
      //   primaryKey: true,
      //   type: Sequelize.INTEGER
      // },
      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');
  }
};

sequelize db:migrate를 실행하면 이전단계 마이그레이션은 실행이 되는데 이 부분에서 오류를 뱉더라구요. 두 파일간에 뭔가 실수가 있는것 같은데 도저히 모르겠습니다.

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

오류메시지는 이렇게 반환됩니다. OrderDetails.orderDetailPriceOrders.totalPrice를 연결시켜주려고 합니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)