Postgres 文档使它看起来像 WHERE 子句可以作为 ON CONFLICT 条件:https : //www.postgresql.org/docs/9.5/static/sql-insert.html
我一直无法让它工作(如果可能的话)。这是我尝试过的众多排列之一:
INSERT INTO friends (id, dob, frn, status, "groupId",
"createdAt", "updatedAt")
VALUES ('1da04305-68ef-4dc1-be6c-
826ab83a6479', '1937-06-01T08:29:08-07:00', 100001, 'New', 'bc1567bc-
14ff-4ba2-b108-4cb2e0f0f768', NOW(), NOW())
ON CONFLICT
WHERE frn=100001 DO NOTHING
Run Code Online (Sandbox Code Playgroud)
frn 没有任何约束,所以语法更简单:
ON CONFLICT (frn) DO NOTHING
Run Code Online (Sandbox Code Playgroud)
引发数据库错误。我希望这是一个简单的语法问题。
我目前正在蛮力这个,但我相信有一个更好的解决方案,使用Sequelize,有问题的代码(使用postgres):
...
then((tile_data) => {
return Encounter.findAll({
where: {
level: tile_data.dataValues.level
},
transaction: transaction_data
}).then((encounter_data) => {
let encounter = encounter_data[Math.floor((Math.random() * encounter_data.length))].dataValues
return Battle.create({
character_id: character_data.dataValues.id,
encounter_id: encounter.id,
encounter_hp: encounter.max_hp,
encounter_mana: encounter.max_mana
}, {
transaction: transaction_data
})
...
Run Code Online (Sandbox Code Playgroud)
除了看似'丑陋',使用这段代码我将所有ENCOUNTERS加载到内存中只是为了从阵列中提取一个元素.
有没有人知道如何通过Sequelize这样做,理想情况下不使用原始查询?
谢谢