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.

set attributes is not updated to database while calling save with patch option

See original GitHub issue

hi all, I found that there is weird issue on save & set and the version of bookshelf.js is 0.10.3

// js code
const model = await Model.forge({ id: 5 }).fetch();
model.set('attr_a', 'some value');
await model.save({ attr_b: 'some value' }, { patch: true });

above operations only update attr_b to database.

In other ORM such as ruby’s ActiveRecord, it will update both attr_a & attr_b

// ruby code
model = Model.find(5);
model.attr_a = 'some value';
model.attr_b = 'some value';
model.save!

Does it make sense that changed attributes should be updated to database no matter what these attributes are changed by set or save method ?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
abagmutcommented, Sep 25, 2020

patch: true is really handy to avoid race condition if you have model updated in few places at the same time (happens for user entity pretty often) you can get some property overridden End up with my own attempt of implementing save fuctionality:

async saveModel(attributes?: Partial<CustomUserModelAttributes>, options?: SaveOptions) {
    if (options) {
        return super.save(attributes, options);
    }

    if (attributes) {
        this.set(attributes);
    }

    if (this.hasChanged()) {
        await super.save(this.changed, { patch: true });
        // dirty hack
        if (attributes) {
            Object.keys(attributes).forEach(key => {
                (this as any)._previousAttributes[key] = attributes[key];
            });
        }
    }
}

The problem is if you call set then save with patch: true and check _previousAttributes you’ll see old value, so all consequence call of mine saveModel method fails. Maybe this can be fixed on library level

0reactions
ricardogracacommented, Sep 28, 2020

Got it. That helps, thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django: save() vs update() to update the database?
But update() does not call save() on models. And if no save() method is called for update() , so the signals are not...
Read more >
Different Ways to Set Attributes in ActiveRecord (Rails 4)
This method used to be called update_attributes in Rails 3. It changes the attributes of the model, checks the validations, and updates the ......
Read more >
Update expressions - Amazon DynamoDB
An update expression specifies how UpdateItem will modify the attributes of an item—for example, setting a scalar value or removing elements from a...
Read more >
Modifying data via the DbContext - Learn Entity Framework Core
When SaveChanges is called, an UPDATE statement is generated and executed by the database. var author = context.Authors.First( ...
Read more >
ActiveRecord::Persistence - Rails API
Any change to the attributes on either instance will affect both instances. ... to the database. Only attribute is updated; the record itself...
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