On Feb 12, 2009, at 6:42 PM, Chris Withers wrote:

>
> Hi All,
>
> With the model from my previous post, I'm looking to find the latest
> Record for a particular Unit.
>
> Someone on #sqlalchemy suggested this:
>
> unit = session.query(Unit).filter(Unit.id==3).first()
>
> record = unit.records.order_by(Record.date.desc()).first()
>
> ...however, this gives me:
>
> AttributeError: 'InstrumentedList' object has no attribute 'order_by'
>
> How should I be looking to do this?


the most straightforward, assuming you'd like to keep this in SQL,  
would be:

session 
.query(Record).with_parent(unit).order_by(Record.date.desc()).first()


alternatively, to do exactly what you stated above, i.e.  
unit.records.order_by()... etc., you can use a "dynamic" relation  
which establishes relations as Query objects.   use lazy="dynamic" for  
this and see the examples in the "Large Collections" portion of the  
documentation.


--~--~---------~--~----~------------~-------~--~----~
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