-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequelize Upsert() return value #3354
Comments
This is only supported by postgres, so to keep the API consistent across dialects this is not possible |
👍 for adding this feature, there's plenty of precedence for dialect-specific features. This seems exactly like |
I also just ran into this problem. I wanted to upsert a list of records, and then do something with them (associate them with another model). That seems to be impossible the way the API is defined now. Is there a recommended workflow for this use case? I'm trying to avoid just recreating all associated records on update, which seems to be the path of least resistance given the API design (particularly that the records must already exist in the DB before they can be associated). |
+1 for adding options.returning on upsert. |
+1, would be tremendously useful |
So I did some digging into this, and it won't be easy. I wanted us to be able to take advantage of At this point I think the best way forward is to append a simple I don't have any more time to work on this for a while, but if anyone wants to take over where I left off feel free! |
+1; this would be useful for me as well. I thought there was an update() function with { upsert: true } as an option but it's totally possible i'm getting confused with Mongoose |
@kylehotchkiss We do have |
I agree it would be nice to have this. Aren't we already implementing dialect specific solutions to do
At any rate, below is my workaround with a mild performance hit: classMethods: {
upsertWithReturn: function (options) {
return this.findOrCreate(options).spread(function (row, created) {
if (created) {
return [row, created];
} else {
return row.updateAttributes(options.defaults).then(function (updated) {
return [updated, created];
});
}
});
},
} I foresee only having race condition issues. However, I am under the impression that Thoughts? |
@angelxmoreno Your code is open to race conditions - But our code isn't 100% perfectly race condition free since technically a parallel connection could change the entry while we run our two sequential queries (but less race prone since there's less roundtrips). |
Does anyone have any updates on this? Would be very nice to have this functionality. |
@Shahor That only applies to the case where the row doesn't already exist though? We can't get the id of an existing row like we can with |
@janmeier You have a point on this. That being said, an ORM should provide abstraction to the developer and make it easy to interact with a database. A workaround to this problem could be something like this comment but shouldn't it be part of the ORM to begin with? |
@Shahor please read #3354 (comment). |
@Shahor @janmeier where Implementing that trick would solve the issue with MySQL and getting the primary key in both inserted and updated case. |
Trying to get inserted record after upsert, but no success even with Sequelize v5 and PostgreSQL. |
Use create() instead of upsert(). It will return you created object. |
any update on this? |
Upsert was recently reimplemented in #12301. Are people still having issues? |
@gabegorelick It is still a |
SQLite also added RETURNING in 2021 BTW: https://www.sqlite.org/lang_returning.html so now this could be solved nicely for it. |
Hello,
I need to get the id for the inserted/updated record when using .upsert() in sequelize.
right now .upsert() returns a boolean indicating whether the row was created or updated.
thanks for your help.
The text was updated successfully, but these errors were encountered: