Skip to content

build and save with associations #9233

Closed

Description

@ro70

What are you doing?

According to http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-build I can build an instance with associations.

const a = await db.A.build({
   id: 1,
   name: 'A',
   B: {
       id: 1,
       user: 2
   }
}, {
   isNewRecord: false,
   include: [db.B]
})
await a.save()

What do you expect to happen?

How can I save this instance with its association into the database?

What is actually happening?

The build seems to work but only the name value is stored into the database not the B.user data.

Dialect: mysql
Dialect version: XXX
Database version: XXX
Sequelize version: 4.28.1
Tested with latest release: No (If yes, specify that version)

Activity

  1. sushantdhiman commented on Mar 23, 2018

    @sushantdhiman
    Contributor

    You have to try with latest version, I remember fixing this

  2. ro70 commented on Mar 23, 2018

    @ro70
    Author

    @sushantdhiman I updated to 4.37.4 but it's still not working.

  3. sushantdhiman commented on Mar 24, 2018

    @sushantdhiman
    Contributor

    It works, here is full testcase, if you think it doesn't may be try presenting a SSCCE

  4. sushantdhiman commented on Mar 24, 2018

    @sushantdhiman
    Contributor

    Ah, may be it builds properly but doesn't save? In any case I will need a SSCCE

  5. sushantdhiman commented on Mar 24, 2018

    @sushantdhiman
    Contributor

    Have you tried this

    const a = await db.A.build({
       id: 1,
       name: 'A',
       B: {
           id: 1,
           user: 2
       }
    }, {
       isNewRecord: false,
       include: [db.B]
    });
    
    await a.save({
      include: [db.B]
    })
  6. ro70 commented on Mar 24, 2018

    @ro70
    Author

    @sushantdhiman Thanks for your reply. The build method is working. The save method seems to have a problem with isNewRecord: false. In this case there are no sql statements for the assocation. I tried both with and without the include option.

    const Sequelize = require('sequelize')
    const sequelize = new Sequelize({username: 'root', password: '', database: 'test', dialect: 'mysql', logging: false})
    
    const A = sequelize.define('A', {name: Sequelize.TEXT})
    const B = sequelize.define('B', {user: Sequelize.INTEGER})
    
    A.belongsTo(B, {foreignKey: 'id'})
    B.hasOne(A, {foreignKey: 'id'})
    
    sequelize.sync({force: true})
    .then(async () => {
      let a = await A.build({
        id: 1,
        name: '',
        B: {
          id: 1,
          user: 1
        }
      }, {
        include: [B]
      })
      console.log('BUILT', a.toJSON())
    
      await a.save()
    
      let reloadedA = await a.reload()
      console.log('RELOADED', reloadedA.toJSON())
    
      a = await A.build({
        id: 1,
        name: 'A',
        B: {
          id: 1,
          user: 2
        }
      }, {
        isNewRecord: false,
        include: [B]
      })
      console.log('BUILT', a.toJSON())
    
      await a.save()
      //await a.save({include: [B]})
    
      reloadedA = await a.reload()
      console.log('RELOADED', reloadedA.toJSON())
    })
    

    Output is

    BUILT { id: 1, name: '', B: { id: 1, user: 1 } }
    Executing (default): INSERT INTO `Bs` (`id`,`user`,`createdAt`,`updatedAt`) VALUES (1,1,'2018-03-24 18:09:37','2018-03-24 18:09:37');
    Executing (default): INSERT INTO `As` (`id`,`name`,`createdAt`,`updatedAt`) VALUES (1,'','2018-03-24 18:09:37','2018-03-24 18:09:37');
    
    Executing (default): SELECT `A`.`id`, `A`.`name`, `A`.`createdAt`, `A`.`updatedAt`, `B`.`id` AS `B.id`, `B`.`user` AS `B.user`, `B`.`createdAt` AS `B.createdAt`, `B`.`updatedAt` AS `B.updatedAt` FROM `As` AS `A` LEFT OUTER JOIN `Bs` AS `B` ON `A`.`id` = `B`.`id` WHERE `A`.`id` = 1;
    RELOADED { id: 1,
      name: '',
      createdAt: 2018-03-24T18:09:37.000Z,
      updatedAt: 2018-03-24T18:09:37.000Z,
      B: 
       { id: 1,
         user: 1,
         createdAt: 2018-03-24T18:09:37.000Z,
         updatedAt: 2018-03-24T18:09:37.000Z } }
    
    BUILT { id: 1, name: 'A', B: { id: 1, user: 2 } }
    Executing (default): UPDATE `As` SET `id`=1,`name`='A',`updatedAt`='2018-03-24 18:09:37' WHERE `id` = 1
    => Missing update statement for the assocation...
    
    Executing (default): SELECT `A`.`id`, `A`.`name`, `A`.`createdAt`, `A`.`updatedAt`, `B`.`id` AS `B.id`, `B`.`user` AS `B.user`, `B`.`createdAt` AS `B.createdAt`, `B`.`updatedAt` AS `B.updatedAt` FROM `As` AS `A` LEFT OUTER JOIN `Bs` AS `B` ON `A`.`id` = `B`.`id` WHERE `A`.`id` = 1;
    RELOADED { id: 1,
      name: 'A',
      createdAt: 2018-03-24T18:09:37.000Z,
      updatedAt: 2018-03-24T18:09:37.000Z,
      B: 
       { id: 1,
         user: 1,
         createdAt: 2018-03-24T18:09:37.000Z,
         updatedAt: 2018-03-24T18:09:37.000Z } }
    
  7. sushantdhiman commented on Mar 25, 2018

    @sushantdhiman
    Contributor

    What you want is update entire includes chain, rather than creating them. This is not supported in Sequelize yet. I will tag this as feature request.

  8. added
    type: feature
    on Mar 25, 2018
  9. ro70 commented on Mar 25, 2018

    @ro70
    Author

    @sushantdhiman Thanks for the information. I think in the meantime I will do like

    a.name = 'B'
    a.B.user = 2
    
    let aPlain = a.get({plain: true})
      
    let queue = []
    queue.push(a.update(aPlain))
    queue.push(a.B.update(aPlain.B))
    await Promise.all(queue)
    

    What do you think?

  10. mirkojotic commented on Jul 4, 2018

    @mirkojotic
    Contributor

    @sushantdhiman Hey, I'd love to tackle this one. I have a use-case for this at work too so its a win/win :D
    All that being said I do need some direction. I've gone through the code, wrote test cases and poked through but I am not 100% sure where to start :) and what needs to be touched.

  11. RoelRoel commented on May 10, 2019

    @RoelRoel

    When I called the save function I thought I could finnaly taking advantage of the fact that I use an ORM and I don't have to write all the update and delete queries myself for the assosiations. Was I disappointed that only the main object was saved. I now still don't understand what the advances are using an ORM like this.... You lose a lot of control and development is nothing faster.....

  12. aat2703 commented on Aug 2, 2019

    @aat2703

    I'm also running in to theese issues.. Also why is there no filtering available on build method?

    For some reason fields: [] option is not available?

  13. papb commented on Aug 2, 2019

    @papb
    Member

    @RoelRoel Although build+save apparently don't work, I know that create works with associations. Have you tried that as a workaround?

  14. papb commented on Aug 2, 2019

    @papb
    Member

    @aat2703

    Why is there no filtering available on build method?

    Can you clarify what you mean by this? What did you want to do that can't be done?

  15. papb commented on Jan 9, 2020

    @papb
    Member

    Duplicate of #6808

  16. 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

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions