[sqlalchemy] Re: Query on a related object's field
Dnia 2009-11-13, Pt o godzinie 05:02 -0800, bojanb pisze: What is the easiest way of getting the equivalent of this: session.query(Someclass).filter_by(related_obj.field=somevalue) Ie. I want to filter by a field of an object that is in relation to objects of Someclass. My original idea was to add related_obj.field as a new relation in the mapper for Someclass but I just couldn't accomplish that. Is there an easier way? http://www.sqlalchemy.org/docs/05/ormtutorial.html#querying-with-joins ? Tomasz Jezierski Tefnet www.tefnet.pl --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query on a related object's field
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) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query on a related object's field
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 -~--~~~~--~~--~--~---