question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

instance `save()` behavior when nothing has changed

See original GitHub issue

Hello,

How should I expect instance.save() to behave when invoked on an instance that has not been modified since load?

From http://docs.sequelizejs.com/en/latest/docs/instances/#updating-saving-persisting-an-instance:

When you call save without changing any attribute, this method will execute nothing;

From http://docs.sequelizejs.com/en/latest/api/instance/#saveoptions-promisethiserrorsvalidationerror:

Validate this instance, and if the validation passes, persist it to the database.

From nodejs (sequelize 3.3.2):

> MyModel.findOne().then(s => s.save());
Executing (default): UPDATE "mymodels" SET "updated_at"='2015-08-15 02:06:57.325 +00:00' WHERE "id" = '3681e9f8-25a5-4863-9015-dd90d3a4a2e2'

If the intended behavior of save is indeed to not update the timestamp (when no fields have changed), how can I achieve that effect, in a future-proof way?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
janmeiercommented, Aug 17, 2015

Yeah, it seems the documentation needs to be cleared up a bit on this point.

It is intentional that nothing is saved if nothing has changed. You can manually mark a column as changed to force a save: instance.changed('updatedAt', true)

1reaction
igraysoncommented, Aug 27, 2015

Okay. It really doesn’t seem like a documentation issue when I compare what you said with the observed behavior.

It is intentional that nothing is saved if nothing has changed.

MyModel.findOne().then(s => s.save());
Executing (default): UPDATE "mymodels" SET "updated_at"='2015-08-15 02:06:57.325 +00:00' WHERE "id" = '3681e9f8-25a5-4863-9015-dd90d3a4a2e2'

(nothing changed; something saved)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do my changes to model instances not get saved ...
The first method's changes get persisted when a save is called. The second method's changes do not get saved. I have noticed this...
Read more >
Why doesn't Django's Model.save() save only the dirty fields ...
I've noticed that Model.save() will update all fields by default, which can introduce a lot of race conditions. If it update only the...
Read more >
Tracking vs. No-Tracking Queries - EF Core | Microsoft Learn
Tracking behavior controls if Entity Framework Core will keep information ... instances and have those changes persisted by SaveChanges() .
Read more >
Strange behavior in model.save() - Google Groups
You assign non saved instance to a meeting. Probably what happened internally is that place.pk is copied to meeting.place_id. > place.save().
Read more >
Working with entity instances — Pony ORM documentation
All created instances belong to the current db_session() . In some object-relational mappers, you are required to call an object's save() method in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found