postgresql sequelize 中 findOrCreate() 和 upsert() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28249394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:46:42  来源:igfitidea点击:

Difference between findOrCreate() and upsert() in sequelize

node.jspostgresqlexpresssequelize.js

提问by fanhats

I was reading over the docand it mentioned two ways of doing an upsert: findOrCreate()or upsert(). I read the descriptions, but I'm still not clear what the difference is.

我正在阅读文档,它提到了两种执行 upsert 的方法:findOrCreate()upsert(). 我阅读了描述,但我仍然不清楚有什么区别。

The upsert()goes on to say:

upsert()接着说:

Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.

Does this mean upsert()explicitly requires I define an idUNIQUE primary field in my model? ie:

这是否意味着upsert()明确要求我id在我的模型中定义一个UNIQUE 主字段?IE:

var Something = sequelize.define("Something", { id: { 
                    type: DataTypes.INTEGER, 
                    primaryKey: true, 
                    unique: true, 
                    allowNull: false}});

If so, why does findOrCreate()not have that restriction?

如果是这样,为什么没有findOrCreate()这个限制?

Can someone explain the use cases for when findOrCreate()and upsert()should be used, and why upsert()has the unique constraint requirement when findOrCreate()doesn't seem to need it?

有人可以解释何时使用findOrCreate()upsert()应该使用的用例,以及为什么upsert()findOrCreate()似乎不需要时具有唯一约束要求?

回答by Yuri Zarubin

.findOrCreatewill query the database with the parameters you provided, and if there is no match, it will perform an insert operation using those same parameters. Whereas the point of an .upsert, is to either update or insert a record. In the case of an update operation, logic dictates that you look for a record based on a unique key, and only once you find this unique record, update its values based on the parameters provided. If you aren't able to find a unique record, only then would you perform an insert.

.findOrCreate将使用您提供的参数查询数据库,如果没有匹配项,它将使用这些相同的参数执行插入操作。而.upsert, 的目的是更新或插入记录。在更新操作的情况下,逻辑指示您根据唯一键查找记录,并且只有在找到此唯一记录后,才根据提供的参数更新其值。如果您无法找到唯一的记录,那么您才会执行插入操作。

I hope that makes sense.

我希望这是有道理的。

回答by noob7

the key difference is Id field. In case of upsert you need the id field. But sometimes, you dont hv the id field. For example when creating a user from an external provider fields say fb, you will not have the id of the user record if it already exists. In this case you will have to use findOrCreate on one of the fields returned by fb like for example email. findOrCreate user where the email is the email returned from fb

主要区别是 Id 字段。在 upsert 的情况下,您需要 id 字段。但有时,您不知道 id 字段。例如,当从外部提供者字段创建用户时,例如 fb,如果用户记录已经存在,您将没有该用户记录的 ID。在这种情况下,您必须在 fb 返回的字段之一上使用 findOrCreate,例如电子邮件。findOrCreate 用户,其中电子邮件是从 fb 返回的电子邮件