Mike Conley wrote:
> I haven't seen how to do this using the relation directly. I do:
>
> session.query(Someclass).join(OtherClass).filter_by(OtherClass.field==somevalue)
>
>
>
>
> I did do some experimenting with a more abstract approach, but did not
> find any need in my application. The only advantage is that it takes
> away the need to know up front what is the name of the other class.
> That might be useful if you are building a framework based on SA, but
> not in most applications.
>
> otherclas = SomeClass.relname.property.mapper
> session.query(Someclass).join(otherclas).filter_by(otherclas.c.field==somevalue)

Another approach is using the has/any methods of the relation:
If SomeClass.relation is many-to-one or one-to-one:
session.query(SomeClass).filter(SomeClass.relation.has(field=somevalue))
or if the relation is one-to-many or many-to-many:
session.query(SomeClass).filter(SomeClass.relation.any(field=somevalue))

This approach produces an EXISTS clause in the SQL, so it's usually
slower than the join approach. On the other hand, it may be easier to
use inside more complicated queries (e.g. inside AND, OR, or NOT
expressions).

-Conor


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