Skip to content

Commit

Permalink
Move the ORDER BY / LIMIT clauses inside the parentheses for compound…
Browse files Browse the repository at this point in the history
… queries.

Fixes #1509.
  • Loading branch information
coleifer committed Feb 22, 2018
1 parent 94aee08 commit 42d9642
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,8 +1752,9 @@ def __sql__(self, ctx):
subquery=False):
ctx.sql(self.rhs)

# Apply ORDER BY, LIMIT, OFFSET.
self._apply_ordering(ctx)
# Apply ORDER BY, LIMIT, OFFSET.
self._apply_ordering(ctx)

return self.apply_alias(ctx)


Expand Down
26 changes: 26 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,3 +1987,29 @@ def test_get_or_create_self_referential_fk2(self):
self.assertEqual(child_db.user.username, 'huey')
self.assertEqual(child_db.parent.name, 'parent')
self.assertEqual(child_db.name, 'child')


class TestCountUnionRegression(ModelTestCase):
@requires_models(User)
def test_count_union(self):
with self.database.atomic():
for i in range(5):
User.create(username='user-%d' % i)

lhs = User.select()
rhs = User.select()
query = (lhs & rhs)
self.assertSQL(query, (
'SELECT "t1"."id", "t1"."username" FROM "users" AS "t1" '
'INTERSECT '
'SELECT "t2"."id", "t2"."username" FROM "users" AS "t2"'), [])

self.assertEqual(query.count(), 5)

query = query.limit(3)
self.assertSQL(query, (
'SELECT "t1"."id", "t1"."username" FROM "users" AS "t1" '
'INTERSECT '
'SELECT "t2"."id", "t2"."username" FROM "users" AS "t2" '
'LIMIT 3'), [])
self.assertEqual(query.count(), 3)

0 comments on commit 42d9642

Please sign in to comment.