在sequelize中使用.upsert()
时需要获取插入/更新记录的id。
现在 .upsert()
返回一个 bool 值,指示该行是创建还是更新。
return db.VenueAddress.upsert({
addressId:address.addressId,
venueId: venue.venueId,
street: address.street,
zipCode: address.zipCode,
venueAddressDeletedAt: null
}).then(function(test){
//test returned here as true or false how can i get the inserted id here so i can insert data in other tables using this new id?
});
最佳答案
我不认为当 OP 提出这个问题时返回插入的记录是可用的,但它已经用这个 PR 实现了。 .从 Sequelize v4.32.1 开始,您可以传递一个 bool 值 returning
作为查询参数,以选择返回一个包含记录的数组和一个 bool 值,或者只是一个指示是否返回的 bool 值或者没有创建新记录。
您仍然需要提供要更新插入的记录的 ID,否则将创建新记录。
例如:
const [record, created] = await Model.upsert(
{ id: 1, name: 'foo' }, // Record to upsert
{ returning: true } // Return upserted record
);
关于node.js - 如何在 Sequelize upsert 中获取插入或更新记录的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063232/