-
Notifications
You must be signed in to change notification settings - Fork 90
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
increment number count using object style #1073
Comments
If there's a number in the database which you want to increment, you can do it with update queries. # If we have this table
class Concert(Table):
tickets_available = Integer() We can change the value like this: # If we have this table
await Concert.update({
Concert.tickets_available: Concert.tickets_available + 1
}).where(Concert.id == some_id) If you have an object, you can do this: concert = await Concert.objects().where(Concert.id == some_id).first()
concert.tickets_available += 1
await concert.save() The problem with the approach above is if there are multiple saves at the same time - the So you're suggesting something like this: concert = await Concert.objects().where(Concert.id == some_id).first()
await concert.increment(Concert.tickets_available)
# Where increment effectively runs this query under the hood:
await Concert.update({
Concert.tickets_available: Concert.tickets_available + 1
}).where(
Concert.id == concert.id
).returning(
Concert.tickets_available
) Yeah, I can definitely see the use case. |
yup, the idea was to have a "visibly separate" syntax for letting the database calculate the increment as apposed to do it in code, which as you mention might produce unexpected results |
I was thinking about it, and the best solution I could come up with is having an |
that would work! |
This is done now - will be in the next release. Thanks for raising this issue, I think it'll be a useful feature. |
good! Thanks for the quick feedback as always! |
It would be good if it was possible to do something like:
that is, using "database-level" number increments together woth "object-style" queries. I can't find any sign of this in the documentation, so I guess only dict-style queries support "server-side" increments atm?
The text was updated successfully, but these errors were encountered: