Skip to content

Upsert to update the association? #6808

Closed

Description

@Abhimanyu310

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

  1. felixfbecker commented on Nov 5, 2016

    @felixfbecker
    Contributor

    I think this is not supported atm, but it would be a nice feature!
    For the time being, you must use separate function calls.

  2. added
    type: feature
    on Nov 5, 2016
  3. zlatinejc commented on Mar 21, 2017

    @zlatinejc

    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 ?

  4. EugineBelfer commented on May 11, 2017

    @EugineBelfer

    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?

  5. stale commented on Jul 11, 2017

    @stale
  6. yussinsharp commented on Jul 25, 2017

    @yussinsharp

    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:

    const p = await Practitioner.findOne({
      where: { id: practitioner.id },
      include: [{ model: Service, as: 'services' }],
    });
    
    // Now we can remove/add more services
    p.services.pop();
    await p.save();
    // p.services.length === 1
    
    // Add by pushing in another service object, if object does not have id, we create
    p.services.push(anotherServiceObject);
    await p.save();
    // p.services.length === 2

    if I don't have this I would have to remove/add by communicating directly to the join table no?

  7. eth0izzle commented on Mar 19, 2018

    @eth0izzle
  8. johncade commented on Apr 15, 2018

    @johncade
  9. joseph-sabido commented on May 3, 2018

    @joseph-sabido
  10. dariospadoni commented on May 25, 2018

    @dariospadoni
  11. vidit-bhatia commented on Jun 15, 2018

    @vidit-bhatia
  12. dennerevaldt commented on Jul 6, 2018

    @dennerevaldt
  13. candreoliveira commented on Aug 5, 2018

    @candreoliveira
  14. 3 remaining items

  15. iamjoyce commented on Sep 20, 2018

    @iamjoyce

    instance.update(..) has the same problem

  16. wilson-adap commented on Dec 10, 2018

    @wilson-adap
  17. lpedromachado commented on Mar 12, 2019

    @lpedromachado
    Contributor

    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?

  18. WTKersten commented on Apr 27, 2019

    @WTKersten
  19. atishayjain commented on May 10, 2019

    @atishayjain
  20. NijsTom commented on May 14, 2019

    @NijsTom
  21. lracicot commented on Jul 26, 2019

    @lracicot
  22. villasv commented on Aug 28, 2019

    @villasv

    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.

  23. papb commented on Aug 28, 2019

    @papb
    Member

    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?

  24. added
    status: awaiting response
    hard
    and removed on Aug 28, 2019
  25. papb commented on Jan 16, 2020

    @papb
    Member

    This has been centralized in #11836

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    hardstatus: awaiting responsetype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions