question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upsert to update the association?

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:40
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
EugineBelfercommented, 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?

6reactions
papbcommented, 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…

await project = Project.upsert({
    description: req.body.description,
    url: req.body.url,
    Faculty: {
        name: req.body.name,
        email: req.body.email
    }
}, {
    include: [{ model: models.FacultyInfo, as: 'Faculty' }]
});
  • 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize update with association - Stack Overflow
First you have to find model including sub model which you want to update. then you can get reference of sub model to...
Read more >
Associations | GORM - GORM
Associations. Auto Create/Update. GORM will auto-save associations and its reference using Upsert when creating/updating a record.
Read more >
How to manage associations during creates/updates?
I have a schema Element with a has_many relationship to Photo. I want to manage this relationship when creating/updating an Element .
Read more >
2957352 - SuccessFactors Integration Center
When you upsert (update) a user on the one-to-many navigation properties, the API always performs a merge operation and appends the values to...
Read more >
Upgrade to v6 - Sequelize
Model​ · options.returning ​ · Model.changed() ​ · Model.bulkCreate() ​ · Model.upsert() ​.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found