Replies: 1 comment 5 replies
-
Yeah, it's an interesting one. Ideally this would be possible: query_1 = Band.select()
query_2 = query_1.where(Band.name == 'Pythonistas')
query_3 = query_2.where(Band.popularity > 2000)
>>> await query_1
>>> await query_2
>>> await query_3 But at the moment, chaining the methods modifies the original query. To change this behaviour, we would have to copy the query each time a method is called on it. The problem with this is it would incur a performance hit. This is a work around (I found some edge cases where it doesn't work for complex queries, but it seems OK for simple queries): import copy
query_1 = Band.select()
query_2 = copy.deepcopy(query_1).where(Band.name == 'Pythonistas') Maybe what we should do is add a query_1 = Band.select()
query_2 = query_1.copy().where(Band.name == 'Pythonistas')
query_3 = query_2.copy().where(Band.popularity > 2000) What do you think? |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am not sure if this is planned behavior, but I was not expecting it. My general thinking is that I ought to be able to chain clauses onto an existing query and issue different variations on a query ... that the query itself would not change unless I re-assign it.
e.g.
Given some query q (I happen to be using a select query. Not sure if this behavior is the same for objects):
If I do something like this:
And then I issue the query subsequently:
My expectation is that the 2nd one will return all of the query results, but what I am seeing is that the where clause seems to have affected the query itself, which will now only return results where somefield > someval.
Is this by design? What is the best way to execute multiple variations of a query? Is there some way to copy the query state so I don't have to rebuild it from scratch?
Edit: I am seeing the same behavior for object queries.
Beta Was this translation helpful? Give feedback.
All reactions