sequelize错误:缺少约束的索引

时间:2018-10-08 05:58:52

标签: mysql node.js foreign-keys migration constraints

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-创建订单

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

当我执行脚本sequelize db:migrate时,以前的迁移没有执行错误。在此级别,它将返回错误。我不知道该怎么解决这个问题。

  

错误:添加外键失败   约束。缺少索引   约束'orderdetails_ibfk_2'在   引用表“订单”

这是错误消息。我想连接文件OrderDetails.orderDetailPriceOrders.totalPrice

谢谢。

2 个答案:

答案 0 :(得分:0)

here报道,似乎Sequelize在引用复合键时存在一些问题。

但是,通过利用Sequelize查询执行,您可以解决问题。在这种情况下,您可以执行以下mysql查询:

ALTER TABLE `db_test`.`OrderDetails` ADD CONSTRAINT `fk_order_detailes_orders` 
FOREIGN KEY (`orderId` , `orderDetailId`) 
REFERENCES `db_test `.`orders`(`orderId` , `totalPrice`);

因此,您的 create-order-detail 迁移文件将变为以下文件:

    'use strict';
    module.exports = {
        up: (queryInterface, Sequelize) => {
            return queryInterface.createTable('OrderDetails', {
                orderDetailId: {
                    type: Sequelize.INTEGER.UNSIGNED,
                    primaryKey: true,
                    allowNull: false,
                    autoIncrement: true,
                },
                orderId: {
                    type: Sequelize.INTEGER,
                    allowNull: false,
                },
                productName: {
                    type: Sequelize.STRING,
                    primaryKey: true,
                    allowNull: false,
                },
                count: {
                    type: Sequelize.INTEGER,
                },
                orderDetailPrice: {
                    type: Sequelize.INTEGER,
                    allowNull: false,
                },
                createdAt: {
                    allowNull: false,
                    type: Sequelize.DATE,
                },
                updatedAt: {
                    allowNull: false,
                    type: Sequelize.DATE,
                },
            })
                .then(() => {
                    return queryInterface.sequelize.query('ALTER TABLE `OrderDetails` ADD ' +
                        'CONSTRAINT `fk_order_details_orders` FOREIGN KEY (`orderId`, `orderDetailPrice`) REFERENCES ' +
                        'Orders(`orderId`, `totalPrice`)');
                });
        },
        down: (queryInterface, Sequelize) => {
            return queryInterface.dropTable('OrderDetails', null);
        },
    };

答案 1 :(得分:0)

佐丹奴,

我以此方式定义了下面的代码。让我知道为什么可以迁移此代码?虽然主键唯一键这两个键都被写入。

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