Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize update record primary key value

I'm trying to update the id value of an existing instance using Sequelize but it is not being persisted to the DB

// model definition
var User = sequelize.define('users', {
  id: {
    type: DataTypes.STRING,
    unique: true,
    primaryKey: true,
    field: 'id'
  }
)};

// usage
User.findById(oldId).then(function(user) {
 user.id = 'new value';
 user.save(); // Id remains the same in DB
});

I'm using sequelize 3.14.0 with node and a PostgreSQL database.

like image 758
Brice Steven Nkengsa Avatar asked Oct 15 '25 18:10

Brice Steven Nkengsa


1 Answers

As others have hinted in the comments, sequelize won't let you update the primary key, see https://github.com/sequelize/sequelize/blob/f482790b85ef2336bb97893c0eaa127d9d6bbc77/lib/instance.js#L334-L336

like image 176
Jan Aagaard Meier Avatar answered Oct 18 '25 08:10

Jan Aagaard Meier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!