Hello! It looks that SQLAlchemy doesn't properly handle union with limits in the following scenario:
res1 = Session.query( Messages ).order_by( Messages.ts ).limit(100) res2 = Session.query( Messages1 ).order_by( Messages1.ts ).limit(100) res3 = res1.union_all( res2 ) SQLAlchemy creates the following final query: SELECT <fields> FROM Messages order by ts limit 100 UNION ALL SELECT <fields> FROM Messages1 order by ts limit 100 Which fails with: ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "UNION" To fix this, both queries should be enclosed in parenthesis: (SELECT <fields> FROM Messages order by ts limit 100 ) UNION ALL ( SELECT <fields> FROM Messages1 order by ts limit 100 ) Best regards Jarek -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.