On Apr 21, 2012, at 7:06 AM, Moritz Schlarb wrote:

> 
> So basically, a Child object may set it's own value for time, but if it's not 
> set, it uses the value from its parent.
> This works, but I would also like to be able to do something like:
> 
> session.query(Child).filter(Child.parent.another_id==2)
> 
> (Which would throw Neither 'InstrumentedAttribute' object nor 'Comparator' 
> object has an attribute 'another_id')

SQLAlchemy doesn't implicitly generate joins when you call attributes from 
relationship-bound attribute (here, Child.parent is a relationship-bound 
attribute and "another_id" is an attribute you're trying to call from it).

instead, you need to think in SQL and tell SQLAlchemy that you'd like to join 
from Child to Parent:

query(Child).join(Child.parent).filter(Parent.another_id==2)

joining is covered in the tutorial at 
http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#querying-with-joins .

Also when you do a query from inside of a @property, you might want to use 
object_session() to get at the Session.  A similar example to what you're doing 
is here:

http://docs.sqlalchemy.org/en/latest/orm/relationships.html#building-query-enabled-properties


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