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

increment number count using object style #1073

Closed
trondhindenes opened this issue Sep 13, 2024 · 6 comments · Fixed by #1081
Closed

increment number count using object style #1073

trondhindenes opened this issue Sep 13, 2024 · 6 comments · Fixed by #1081

Comments

@trondhindenes
Copy link

It would be good if it was possible to do something like:

band = await Band.objects().get(Band.name == 'Pythonistas')
assert band.count == 1
band.count.increment(1)
band = await band.update()
assert band.count == 2

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?

@dantownsend
Copy link
Member

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 tickets_available might be incorrect.

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.

@trondhindenes
Copy link
Author

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

@dantownsend
Copy link
Member

I was thinking about it, and the best solution I could come up with is having an update_self method, which just updates the object it's called on. It's more flexible than having dedicated increment / decrement methods.

@trondhindenes
Copy link
Author

that would work!

@dantownsend
Copy link
Member

dantownsend commented Sep 21, 2024

This is done now - will be in the next release.

Thanks for raising this issue, I think it'll be a useful feature.

@trondhindenes
Copy link
Author

good! Thanks for the quick feedback as always!

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

Successfully merging a pull request may close this issue.

2 participants