Richard
Richard

Reputation: 16742

Why doesn't save() work in Sequelize?

I call save() on an object that already exists in my database and has a primary key defined.

Instead of updating the object, Sequelize tells me id must be unique. Why?

I don't want to have to say update( {every,single,field,except,the,id}). That's so tedious.

Here's my current code:

const instance = db.dbo__peach_payments_transactions.build(existingPayment);
return db.transaction( async (transaction) => {
  return instance.save();
});

I've also now tried:

return instance.update({fields: [
          'updated_at',
          'orders_id',
          'payment_states_id',
          'response_data',
          'token',
          'result',
          'reason_code',
          'reason_message',
          'price',
          'processed_at']});
      });

but this generates the opposite sql, instead of saving those fields, it excludes them giving me the error: updated_at cannot be null (even when it's not null) with this sql:

"INSERT INTO "dbo"."peach_payments_transactions" ("peach_payments_transactions_id","created_at") VALUES ('7fb301e9-bb72-4a70-9ee5-0db1376e83e8','2016-07-11 13:48:20.137 +00:00') RETURNING *;"

Upvotes: 2

Views: 3025

Answers (1)

demarchisd
demarchisd

Reputation: 732

Could you try setting instance.isNewRecord to false before sending it to the server?

Maybe sequelize is considering it a new record, therefore it tries to insert it instead of updating

Upvotes: 5

Related Questions