Repository navigation
Description
I have a Project model and a FacultyInfo Model and the relation between the two is:
Project.belongsTo(models.FacultyInfo, {
as : 'Faculty',
onDelete: "CASCADE"
})Now when creating the models, I use a web form to submit the data and create both the models simultaneously as below:
var project = models.Project.create({
description: req.body.description,
url: req.body.url,
// ...
Faculty: {
name: req.body.name,
email: req.body.email,
}
}, {
include: [ {model: models.FacultyInfo, as: 'Faculty'} ]
}).then(function (task) {
//
});And this works perfectly. Now I want to do an upsert in a similar way. In the above code, if I replace create with upsert, and pass in the primary key for the Project as an argument, the Project model gets updated (if found), but the FacultyInfo model does not get updated. And if the project was not found, it gets created (as expected), but the FacultyInfo model does not get created, and the faculty_id in the projects table which is the foreign key stays at Null
Maybe, there is a way to get upsert working the way I want, but I haven't been able to find it, and I've searched so many posts on github/stackoverflow and what not.
I would appreciate if someone would show me how to achieve this, or show me an alternative way to achieve this.
Activity
felixfbecker commentedon Nov 5, 2016
I think this is not supported atm, but it would be a nice feature!
For the time being, you must use separate function calls.
zlatinejc commentedon Mar 21, 2017
So this feature would mean that you could actually update associated models when passed as an attribute, correct via simple save() call to the parent entity model ?
EugineBelfer commentedon May 11, 2017
It would be the greatest feature ever!
Cause for current moment I can easily CREATE instance with associations(using INCLUDE option).
But if I want to update instance with some changes in instances, associated with parent one, it become really painful.
Or maybe I don't know how to do it. Any ideas, guys?
stale commentedon Jul 11, 2017
yussinsharp commentedon Jul 25, 2017
I agree! I had this functionality in a RethinkDB ORM called Thinky and it made my API endpoints really clean and simple. I have a Practitioner-Service m2m relationship and it would be great if I could do:
if I don't have this I would have to remove/add by communicating directly to the join table no?
eth0izzle commentedon Mar 19, 2018
johncade commentedon Apr 15, 2018
joseph-sabido commentedon May 3, 2018
dariospadoni commentedon May 25, 2018
vidit-bhatia commentedon Jun 15, 2018
dennerevaldt commentedon Jul 6, 2018
candreoliveira commentedon Aug 5, 2018
3 remaining items
iamjoyce commentedon Sep 20, 2018
instance.update(..)has the same problemwilson-adap commentedon Dec 10, 2018
lpedromachado commentedon Mar 12, 2019
I too need this badly, but given that it is not possible with 'upsert' (nor with 'findOrCreate'), how can we achieve what the OP wants with what Sequelize offers?
WTKersten commentedon Apr 27, 2019
atishayjain commentedon May 10, 2019
NijsTom commentedon May 14, 2019
lracicot commentedon Jul 26, 2019
villasv commentedon Aug 28, 2019
Stop commenting with
+1, this is creating needless notifications for everyone in the discussion. Just use the 👍 react on the issue at the top so Sequelize maintainers can use this directly to gauge community interests.As for the feature request, is there any know implementation of a workaround that is generic enough to be reused elsewhere? Maybe that could be the starting point for the feature.
papb commentedon Aug 28, 2019
Hi everyone, I think the next step to make this feature come true is to begin suggesting what exactly it should do. I see many potential edge cases arising...
So, which SQL query exactly would you like my code above to generate?
Is there any implementation difference for HasOne / BelongsTo / HasMany / BelongsToMany here? Probably yes. How to detect if one associated entry is one that already exists but is being updated, or a new one?
papb commentedon Jan 16, 2020
This has been centralized in #11836