我正在尝试使用两个模型之间的多对多关联来运行一个简单的create
调用,但是当我尝试将现有模型添加为关联时,却遇到了唯一约束错误。
我尝试调用不带include的create,然后调用setReward方法,这确实起作用。我确实知道,通过检查续集lib / model.js,我关联的模型始终将isNewRecord属性设置为true。
const reward = await db.reward.create({
title: 'reward 1', ...
});
const level = await db.level.create({
title: 'level 1', rewards: [reward], ...
}, {
include: [{
model: db.reward,
as: 'rewards'
}]
});
这是输出的一部分。 Sequelize正在尝试保存我已经保存的模型。
Executing (default): INSERT INTO `reward` (`id`,`fid`,`type`,`title`,`description`,`unlockable`,`unlocked`,`created_at`,`updated_at`) VALUES ('53428154-251c-4f01-92cd-4724f0bd3fe9','test','PROFILE_BADGE','Reward 1','Reward 1',true,false,'2019-09-04 13:24:28','2019-09-0413:24:28');
Executing (default): INSERT INTO `level` (`id`,`fid`,`order`,`title`,`description`,`created_at`,`updated_at`) VALUES ('17ed2675-0754-48df-93a6-53d9d770189d','test',1,'Level 1','Level 1','2019-09-04 13:24:28','2019-09-04 13:24:28');
Executing (default): INSERT INTO `reward` (`id`,`fid`,`type`,`title`,`description`,`unlockable`,`unlocked`,`created_at`,`updated_at`) VALUES ('53428154-251c-4f01-92cd-4724f0bd3fe9','test','PROFILE_BADGE','Reward 1','Reward 1',true,false,'2019-09-04 13:24:28','2019-09-0413:24:28');
{ SequelizeUniqueConstraintError: Validation error
at Query.formatError (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\sequelize\lib\dialects\mysql\query.js:226:16)
at Query.handler [as onResult] (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\sequelize\lib\dialects\mysql\query.js:68:23)
at Query.execute (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\mysql2\lib\commands\command.js:30:14)
at Connection.handlePacket (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\mysql2\lib\connection.js:449:32)
at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\mysql2\lib\connection.js:72:12)
at PacketParser.executeStart (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.Connection.stream.on.data (C:\Users\jamesg\Documents\dtl-api-profile\node_modules\mysql2\lib\connection.js:79:25)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'SequelizeUniqueConstraintError',
...