New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
model.save() attempts to update and then throws error when hasChanged() is false #372
Comments
Where would this help you avoid a query in the real world? |
Here's my use case: Everytime a user logs in with oauth I have to query the If it doesn't fetch a record, I create a new one. If the record doesn't have an associated On the If I'm creating the On the At this point nothing has actually changed most of the time because most of the time it's returning users. But sometimes it's new users. And occasionally the returning user has changed their info. Then I want to call I'm familiar with shortcuts like Side question: Is it possible to In generally I've just got a lot of new/dirty/exists if-branching that feels (to me) like it belongs in the ORM rather than in the application. Another aside: If you have time for an hour of consulting sometime next week, I'd really like to have you take a look at my code, tell me what I'm doing "the wrong way" and explan the "right way" to me. I'm imagining that when I'm done with this specific part of my project there will only be 3 tables and maybe 500 lines of actual code. |
Hmm. Read through all that and I think I at least understand the gist of what you're doing. I'd actually contend the opposite on built in versus userland in this case. Detecting changes is tricky because you need to fetch the entire model first in order to really know for sure whether a Oftentimes it's not a bad idea to just try to keep operations idempotent and ignore the possibility of unnecessary writes. But unless it's a huge issue, it's often better to just accept the additional response time and address the issue comprehensively in the future with some sort of caching strategy. Re: consulting, I'm happy to spend some time digging in. Email's on my profile. Also, be careful with your conditional in your example. You may be releasing zalgo. |
The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year. If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it. We also started an open discussion about the future of Bookshelf.js here #1600. Feel free to drop by and give us your opinion. |
I would prefer that
model.save()
graciously become a noop and neglect to run the sql statement whenhasChanged()
returnsfalse
, but still call the success callback.Pretty:
vs
Ugly:
Perhaps there's another method
saveUnlessUnchanged()
or perhaps aunlessChanged: true
option I could pass tosave()
that I don't know about?The practical reason behind this is that I'm taking data in from a third party source that may or may not have changed since the last time I queried. Also, there are many cases where an array of user data may come in with some elements changed and some left as they were.
The text was updated successfully, but these errors were encountered: