Behavior of limit in own statement differs from when chaining?

8 views
Skip to first unread message

Michael Parker

unread,
Nov 25, 2012, 9:25:02 PM11/25/12
to sqlal...@googlegroups.com
Hi all,

For the following code, which uses tables for CalendarEntry, Match, and Team entities:

team_alias1 = aliased(Team)
team_alias2 = aliased(Team)
matches_query = session\
.query(CalendarEntry.match_id, Match, team_alias1, team_alias2)\
.join(Match, CalendarEntry.match_id == Match.id)\
.join(team_alias1, Match.team1_id == team_alias1.id)\
.join(team_alias2, Match.team2_id == team_alias2.id)\
.filter(CalendarEntry.user_id == client_id)\
.limit(2)

When setting echo=True and running my unit tests in SQLite, I get a query that looks like:

SELECT "CalendarEntries".match_id AS "CalendarEntries_match_id", "Matches".id AS "Matches_id", ... etc 
WHERE "CalendarEntries".user_id = ? LIMIT ? OFFSET ?
(1, 2, 0)

Which correctly returns 2 CalendarEntry objects. However, if I no longer chain the call to limit immediately, but put it in its own statement, like so:

matches_query = session\
.query(CalendarEntry.match_id, Match, team_alias1, team_alias2)\
.join(Match, CalendarEntry.match_id == Match.id)\
.join(team_alias1, Match.team1_id == team_alias1.id)\
.join(team_alias2, Match.team2_id == team_alias2.id)\
.filter(CalendarEntry.user_id == client_id)
matches_query.limit(2)

Then I get a query without a LIMIT, which returns all CalendarEntry objects:

SELECT "CalendarEntries".match_id AS "CalendarEntries_match_id", "Matches".id AS "Matches_id", ... etc 
WHERE "CalendarEntries".user_id = ?
(1,)

Can someone explain what's going on, and how to fix this?

Thanks,
Mike

Michael Parker

unread,
Nov 25, 2012, 9:41:04 PM11/25/12
to sqlal...@googlegroups.com
I just realized that I have to assign the returned value of matches_query.limit(2) back to matches_query. Sorry for the stupid question everyone :( Maybe I should take a break from programming... forever.

- Mike





--
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/-/AsqSgGMMBxoJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages