> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:sqlalch...@googlegroups.com] On Behalf Of john smallberries
> Sent: 24 August 2009 08:51
> To: sqlalchemy
> Subject: [sqlalchemy] negative indexes in query slices?
> 
> 
> I just tried limiting a query to the final 10 items of a 30010 item
> table using this slice notation:
>   q = some.Session.query()
>   items = q[-10:]
> the resulting mysql query had no limit or offset clause. Changing the
> form to
>   items = q[q.count()-10:]
> produced:
>   select from data LIMIT 30000, 18446744073709551615
> (which worked fine in practice). Changing the form to
>   items = q[q.count()-10:q.count()]
> produced the desired:
>   select from data LIMIT 30000, 10
> 
> Is that the expected behavior for negative indices?
> I am using SA 0.5.4p2

I think the problem is that there is no way of specifiying negative
indices in the SQL LIMIT clause (at least for MySQL - I don't know about
other databases), so it would have to be emulated in some way. The only
choices I can think of are:

a) Execute a count() first, as you did above

b) Use a subquery in which the sort order is reversed

Both would have to transform the query substantially, so I'm not sure if
it's the sort of thing that SA should do automatically.

Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to