Peewee: reducing where conditionals break after a certain length

488 views Asked by At

This is what I have:

SomeTable.select.where(reduce(operator.or_, (SomeTable.stuff == entry for entry in big_list)))

The problem arises when I have a relatively large list of elements in big_list and I get this:

RuntimeError: maximum recursion depth exceeded

Is there another way to approach this that doesn't involve splitting up the list into several chunks?

Tried the suggestion to use any, here's my error:

Traceback (most recent call last):
  File "C:/Users/f9xk3li/Documents/GitHub/leoshop_web/leoshop_web/data_models/data_model.py", line 347, in <module>
    search_bins_all("BoA 0")
  File "C:/Users/f9xk3li/Documents/GitHub/leoshop_web/leoshop_web/data_models/data_model.py", line 179, in search_bins_all
    for d in generator.order_by(SomeTable.RetrievedDate.desc()):
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 282, in inner
    clone = self.clone()  # Assumes object implements `clone`.
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2202, in clone
    return self._clone_attributes(query)
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2412, in _clone_attributes
    query = super(SelectQuery, self)._clone_attributes(query)
  File "C:\Users\f9xk3li\AppData\Local\Continuum\Anaconda\lib\site-packages\peewee.py", line 2206, in _clone_attributes
    query._where = self._where.clone()
AttributeError: 'bool' object has no attribute 'clone'

And here's the code

generator = SomeTable.select()
generator = generator.where(any(SomeTable.BIN == entry for entry in big_list))
for d in generator:
    ....
2

There are 2 answers

5
Jacob Zimmerman On BEST ANSWER

Try ...where(SomeTable.BIN.in_(big_list))

PeeWee has restrictions as to what can be used in their where clause in order to work with the library.

http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators

3
coleifer On

To expand on Jacob's comment on the approved answer, I think he's saying you can use subqueries rather than resolving all the IDs.

E.G.

admin_users = User.select().where(User.is_admin == True)
admin_messages = Message.select().where(Message.user.in_(admin_users))