【发布时间】:2018-12-19 12:58:52
【问题描述】:
我正在使用 Sails v1.1 -
我按照此处的风帆文档创建了一个多对多的自定义模型关联 - https://sailsjs.com/documentation/concepts/models-and-orm/associations/through-associations
PetUser 模型有两列 pet 和 user,其中每一列都是各自的 id。我想创建一个唯一的多键索引,这意味着不能有两行具有相同的“宠物和用户”组合。意味着第二次调用应该成功,第三次调用应该失败并出现唯一性错误:
await PetUser.create({ user: 1, pet: 33 }); // should succeed
await PetUser.create({ user: 1, pet: 44 }); // should succeed as user/pet combination is different
await PetUser.create({ user: 1, pet: 33 }); // should fail
我尝试将unique: true 添加到下面PetUser 模型上的owner 和pet 属性,但只有第一个unique: true 得到尊重。
这是我在myApp/api/models/PetUser.js中的代码
module.exports = {
attributes: {
owner: {
model:'user',
unique: true
},
pet: {
model: 'pet',
unique: true
}
}
}
【问题讨论】:
标签: sails.js