I haven't boiled this down to a short test case yet, but when my WHERE clause gets especially long I start getting the "recursion depth exceeded" exception. Is this a well-known limitation of sqlalchemy? We're running this query in production currently without SQLAlchemy, and it performs fine, but perhaps I need to look for another approach...
If I keep the filter condition relatively short, my query looks like this and runs fine (with fake columns start_date, X, Y, and Z on table T): SELECT X, sum(Z) AS Z FROM T WHERE T.start_date >= :start_date_1 AND T.start_date <= :start_date_2 AND NOT (T.X = :X_1 AND T.Y = :Y_1) AND NOT (T.X = :X_2 AND T.Y = :Y_2) AND NOT (T.X = :X_3 AND T.Y = :Y_3) GROUP BY T.X However, if I make the filter() clause very long (over 150 AND NOT... clauses), I start getting exceptions with this stack trace: File "test.py", line 350, in do_test print q File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 3031, in __str__ return str(self._compile_context().statement) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/expression.py", line 1790, in __str__ return unicode(self.compile()).encode('ascii', 'backslashreplace') File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/expression.py", line 1778, in compile return self._compiler(dialect, bind=bind, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/expression.py", line 1784, in _compiler return dialect.statement_compiler(dialect, self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 277, in __init__ engine.Compiled.__init__(self, dialect, statement, **kwargs) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 705, in __init__ self.string = self.process(self.statement) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 724, in process return obj._compiler_dispatch(self, **kwargs) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 941, in visit_select t = select._whereclause._compiler_dispatch(self, **kwargs) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in visit_clauselist for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 475, in <genexpr> s for s in File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in <genexpr> for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in visit_clauselist for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 475, in <genexpr> s for s in File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in <genexpr> for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in visit_clauselist for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 475, in <genexpr> s for s in File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in <genexpr> for c in clauselist.clauses) ... File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 475, in <genexpr> s for s in File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in <genexpr> for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in visit_clauselist for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 475, in <genexpr> s for s in File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 477, in <genexpr> for c in clauselist.clauses) File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 72, in _compiler_dispatch return getter(visitor)(self, **kw) RuntimeError: maximum recursion depth exceeded -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/FeiLu3iVqisJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.