Skip to content
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

Upsert method #381

Closed
zacharynevin opened this issue Jun 8, 2014 · 7 comments
Closed

Upsert method #381

zacharynevin opened this issue Jun 8, 2014 · 7 comments
Labels

Comments

@zacharynevin
Copy link

A lot of times I run into the issue of having to insert a record if it doesn't exist, and update if it doesn't. Is there an upsert method in bookshelf? If not, what would be the cleanest way to do this?

@bendrucker
Copy link
Member

No but you can track it at knex/knex#54. It would be appreciated if you could share your exact use case there. This is something I'm pretty sure I've never needed even once and I believe Tim mentioned the same somewhere. That's not to say it's invalid — would just be helpful to have a hint of why people want it and how they'd use it (versus a bunch of 👍 in the thread).

@zacharynevin
Copy link
Author

Let's say that I want to have the ability to save or update, for example, thebbilling address of a user. Instead of having two api methods for create and update, I have one that does both with an upsert.

@bendrucker
Copy link
Member

Can you be a little more detailed about your schema? Normally Bookshelf figures out whether a model is new based on an id.

@zacharynevin
Copy link
Author

Oh, I never knew that.

@strarsis
Copy link

strarsis commented Sep 9, 2016

Possibly related: #1333

@badave
Copy link

badave commented Mar 17, 2017

@bendrucker I have a good example - let's say you have a set of calculations you have to perform and you're optimizing so you save the value of the calculated into a table as a relation to the first model -- using the ID of the first model in a second table -- you can upsert using that related model's ID and the result. It's not a problem that you are blowing away the previous result because it's the correct, up to date amount and you don't care if the previous amount existed or is updated.

It saves you a round trip to the database.

@strarsis
Copy link

bookshelf doesn't really seem to support upsert at all because the underlying knex doesn't support it either, see knex/knex#54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants