Felix Schwarz wrote:
> Hi,
> 
> after reading the docs [1] I thought that something like
> session.query(User).filter(User.c.id > 3)[0]
> should work even if the filter clause does not return any rows.
> 
> But - compliant with Python's behavior - SQLAlchemy raises an
> IndexError.
> (...)
>    File 
> "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py", 
> line 577, in __getitem__
>      return list(self[item:item+1])[0]
> IndexError: list index out of range
> I expected that [0] applied to query without results would 
> return None.
> 
> Did I miss another possibility for LIMIT'ing queries (using 
> sqlalchemy.orm)?
> 
> fs
> 
> [1] 
> http://www.sqlalchemy.org/docs/04/ormtutorial.html#datamapping
> _querying

I think being matching Python's behaviour is definitely the right way to
go. If you want to get None specifically for index [0], you can use the
'first' method on query.

If you are happy get an empty list, you could use a slice and then call
.all()

Eg:

session.query(User).filter(User.c.id > 3)[:1].all()

-> []

Hope that helps,

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to