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
,以前的迁移没有执行错误。在这个级别,它返回错误。我不知道该怎么解决这个问题我想它出了问题。
错误:添加外键约束失败。引用表“orders”中缺少约束“orderdetails\u ibfk\u 2”的索引
这是错误消息。我想连接文件 OrderDetails.orderDetailPrice
以及 Orders.totalPrice
.
谢谢。
2条答案
按热度按时间hrirmatl1#
乔达诺,
我这样定义代码如下。让我知道为什么这个代码可以迁移?主键和唯一键这两个键都是写的。。
创建-order-detail.js
qvtsj1bj2#
如本文所述,sequelize在引用复合键时似乎存在一些问题。
然而,通过利用sequelize查询执行,您可以解决您的问题。在这种情况下,您可以执行以下mysql查询:
因此,您的“创建订单明细”迁移文件将变为以下内容: