我使用Sequelize具有以下这种结构的模型:
org.eclipse.rdf4j.query.algebra.evaluation.function.Function
当API发出 put 请求并发送JSON文件时,我需要在数据库中同时更新“公司”和“地址”。
插入代码可同时使用:
class Company extends Model {
static init(sequelize) {
super.init({
name: { type: DataTypes.STRING },
email: { type: DataTypes.STRING }
}, {
sequelize
});
}
static associate(models) {
this.HasMany(models.Address, { foreignKey: 'company_id', as: 'addresses' })
}
}
class Address extends Model {
static init(sequelize) {
super.init({
street: { type: DataTypes.STRING },
zipcode: { type: DataTypes.STRING },
city: { type: DataTypes.STRING }
}, {
sequelize
});
}
static associate(models) {
this.BelongsTo(models.Company, { foreignKey: 'company_id', as: 'company' })
}
}
但是,当使用更新方法时,它仅更新“公司”的数据,而“地址”不变。
有人经历过这个问题并且有解决方案吗?我在《续集》手册中找不到任何内容
答案 0 :(得分:0)
IRL,我真的不知道associated-create选项的工作方式。据我所知,SQL无法立即插入多个表中。所以,我想这会发生一些不可思议的事情。
WRT更新,您将陷于一个良好的老式多步骤保存/创建/更新过程。我不知道您的约束是什么,但请查看associative setter。这是一个破坏性的小虫子,所以要当心。但是,如果用户可以在该PUT调用中更新,添加或删除地址,这将特别有用。
另外,值得putting this whole effort on one transaction。这不会使查询便宜。但是,如果出现问题,它们会一起回滚。