如何使用对象创建具有现有关联的条目?
例如,
User.create({
socialMedia: [{
socialId: 1, //should reference existing social media
userSocialMedia: {
name: 'foo' //should create new "through" entry
}
}],
bank: {
bankId: 1 //should reference existing bank
});
Run Code Online (Sandbox Code Playgroud)
我可以做User.create({ bankId: 1 }),但是由于从客户端发送的数据是这种形式,有没有办法告诉 sequelize 是为每个包含的模型添加新的还是使用现有的?
执行此操作的一种方法是使用外键属性名称并直接提供 ID,而不是通过嵌套对象。在您的示例中,连接列可能称为bankId。
您可以将用户创建为:
{
socialMedia: [{
socialId: 1, //should reference existing social media
userSocialMedia: {
name: 'foo' //should create new "through" entry
}
}],
bankId: 1 //should reference existing bank
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3268 次 |
| 最近记录: |