我有2个表,通过交集表有多对多的关系。
产品 - >订单< - Outlets
这就是Sequelize所生成的
CREATE TABLE IF NOT EXISTS `Orders` (`id` INTEGER auto_increment , `amount` INTEGER, `orderDate` DATETIME, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `OutletId` INTEGER, `ProductId` INTEGER, `UserId` INTEGER, UNIQUE `Orders_ProductId_OutletId_unique` (`OutletId`, `ProductId`), PRIMARY KEY (`id`), FOREIGN KEY (`OutletId`) REFERENCES `Outlets` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`ProductId`) REFERENCES `Products` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`UserId`) REFERENCES `Users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
我知道它会创建一个唯一的键:UNIQUE Orders_ProductId_OutletId_unique(OutletId,ProductId)。
这就是为什么我无法保存,使用相同的OutletId和ProductId,但是,我的情况,OutletId和ProductId可以相同,但orderDate必须不同。
所以我保存这个没关系,
OrderId, ProductId, OutletId, orderDate
1, 1, 1, '2016-1-1'
2, 2, 1, '2016-1-1'
但是,当我保存另一行时:
1, 1, 1, '2016-1-2'
MySql给了我这个错误:
ERROR 1062: 1062: Duplicate entry '1-1' for key 'Orders_ProductId_OutletId_unique'
好的,问题是,如何创建具有此验证或约束的模型?
以下是我的模特:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Order = sequelize.define('Order', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
amount: DataTypes.INTEGER,
orderDate: DataTypes.DATE
},
{
associate: function(models){
Order.belongsTo(models.Outlet);
Order.belongsTo(models.Product);
Order.belongsTo(models.User);
}
}
);
return Order;
};
'use strict';
module.exports = function(sequelize, DataTypes) {
var Product = sequelize.define('Product', {
inventoryCode: DataTypes.STRING,
name: DataTypes.STRING,
nameKh: DataTypes.STRING,
unitKh: DataTypes.STRING,
unit: DataTypes.STRING,
monthlyCaseTarget: DataTypes.INTEGER,
pieces: DataTypes.INTEGER,
star: DataTypes.BOOLEAN,
price: DataTypes.FLOAT,
active: DataTypes.BOOLEAN
},
{
associate: function(models){
Product.belongsToMany(models.Outlet, {through: models.Order});
Product.belongsTo(models.Category);
// Product.hasMany(models.Order);
}
}
);
return Product;
};
'use strict';
module.exports = function(sequelize, DataTypes) {
var Outlet = sequelize.define('Outlet', {
outletCode: DataTypes.STRING,
outletName: DataTypes.STRING,
outletNameKh: DataTypes.STRING,
outletSubtype: DataTypes.STRING,
perfectStoreType: DataTypes.STRING,
address: DataTypes.STRING
},
{
associate: function(models){
Outlet.belongsToMany(models.Product, {through: models.Order});
Outlet.belongsTo(models.Distributor);
// Outlet.hasMany(models.Order);
}
}
);
return Outlet;
};
答案 0 :(得分:0)
由于sequelize产生了这个查询:
UNIQUE Orders_ProductId_OutletId_unique (OutletId, ProductId)
所以我有点通过在订单列中添加复合唯一键来破解它。
'use strict';
module.exports = function(sequelize, DataTypes) {
var Order = sequelize.define('Order', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
amount: {
type: DataTypes.INTEGER
},
orderDate: {
type: DataTypes.DATE,
unique: 'Orders_ProductId_OutletId_unique'
}
},
{
associate: function(models){
Order.belongsTo(models.Outlet);
Order.belongsTo(models.Product);
Order.belongsTo(models.User);
}
}
);
return Order;
};