node.js - 如何在 Sequelize upsert 中获取插入或更新记录的 ID?

标签 node.js sequelize.js

在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/

相关文章:

Node.js 到 Flume-NG

node.js - Postgre 的 BeforeCreate 钩子(Hook)不使用 bcryptjs 对密码进行哈希处理

node.js - Sequelizejs 中的索引表

mysql - 如何在nodejs中执行sequelize批量插入

node.js - 无法加载文件或程序集“System.ValueTuple Visual Studio 2017 15.8 Node js 项目”

windows - 如何在 Apache 和 Win7 下使用 node.js 在端口 80 上创建服务器?

javascript - 你可以存储对 mongo 集合的引用吗?

node.js - Docker 在生产中组合

javascript - sequelize.js - GetSomething() 在 hasOne 关系中返回错误值,但在 BelongTo 中返回正确值

javascript - 为什么我只得到一个与此模型的关联?