Skip to content

To use update model in models with associations #7703

Closed

Description

@4inn

Hi, I need to update the records of a db using the model update method ..

I really do not know if it's a mistake or misuse ..

They are two models associated .. the problem is that I only update the first model .. does not even run the sql statement to update the second model ..

Models

// --  Define pUserData Model
const pUserData = sequelize.define('userData', {
  user_xuuid : {
    type : Sequelize.UUID,
    primaryKey : true,
    defaultValue : Sequelize.UUIDV4,
    allowNull : false
  },
  fullname : {
    type : Sequelize.STRING
  },
  created_at : {
    type : Sequelize.DATE,
  },
  updated_at : {
    type : Sequelize.DATE,
  }
}, {
  underscored : true
});

// --  Define pUser Model
const pUser = sequelize.define('user', {
  xuuid : {
    type : Sequelize.UUID,
    primaryKey : true,
    defaultValue : Sequelize.UUIDV4,
    allowNull : false
  },
  username : {
    type : Sequelize.STRING
  },
  created_at : {
    type : Sequelize.DATE,
  },
  updated_at : {
    type : Sequelize.DATE,
  }
}, {
  underscored : true
});

Define Association

pUserData.pUser = pUserData.belongsTo(pUser);

Update Model

pUserData.update({
	fullname : req.body.fullname,
	user : {
		username : req.body.username,
	}
}, {
	where : {
		user_xuuid : req.body.xuuid,
	}
}, {
	include: [ uUserData.pUser ]
}).then(data => {
	// data ..
})

Thanks!

Activity

  1. stale commented on Jul 28, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  2. Jayendran-p commented on Aug 3, 2017

    @Jayendran-p

    Would like to have the same functionality

  3. perrosnk commented on Dec 20, 2017

    @perrosnk

    @4inn Have you found any solution to this?

  4. MikeDabrowski commented on Mar 18, 2018

    @MikeDabrowski

    @4inn @perrosnk any news about this issue?

  5. ghozalisara commented on Jan 15, 2020

    @ghozalisara

    Any news ?

  6. reopened this on Jan 16, 2020
  7. 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions