2 days trying by myself... 20 minutes with your answer... I still
don't know why I didn't try what you said...

Thank you, Michael

2012/5/16 Michael Bayer <mike...@zzzcomputing.com>:
>
> On May 16, 2012, at 5:50 PM, Hector Blanco wrote:
>
>> Hello everybody!
>>
>> I would like to know if it's possible to create an optimized query
>> that gives all the elements of a class A that have a relationship to
>> an specific class B
>
> from the below it seems like you mean "a specific *instance* B"...
>
>> As you can see, the Regions don't have a backref to Layout (mainly
>> because they may be used in other classes) but one specific region
>> will only be in one layout at the same time (regions are never shared
>> by layouts) and one Region will never be used in the same layout more
>> than once (there won't be a Layout.logoRegion and a
>> Layout.mainImageRegion pointing to the same Region instance)
>>
>> What I would like to do is knowing what layouts (or, better said,
>> "layout") are using an specific given region in any of its fields.
>>
>> Let's say, given this:
>>
>> Layouts:
>>
>> +----+-------+--------+----------------+----------------------+----------------------+
>> | id | width | height | logo_region_id | background_region_id |
>> main_image_region_id |
>> +----+-------+--------+----------------+----------------------+----------------------+
>> | 38 |  1280 |    720 |              8 |                    7 |
>>            9 |
>> | 45 |  1280 |    720 |             15 |                   16 |
>>           17 |
>> | 52 |  1280 |    720 |             23 |                   24 |
>>           25 |
>> +----+-------+--------+----------------+----------------------+----------------------+
>>
>>
>> I would like to have a method to say "What layouts are using region
>> '16'?"
>
> so say you have region 16 - "what layouts"?  query for layouts:
>
> region = s.query(Region).get(16)
> s.query(Layout).filter(or_(
>    Layout.logoRegion==region,
>    Layout.mainImageRegion==region,
>    Layout.backgroundRegion==region
> ))
>
> basically SomeClass.some_m2o_relationship==some_instance will pull out the PK 
> of some_instance and compare it to the FK column on SomeClass.
>
> If you had the numerical id of some_instance instead, you'd adjust this to 
> compare on the column, not the relationship.
>
> this would be perfect for a class method also:
>
> class Layout(...):
>    ....
>
>   @classmethod
>   def has_region(cls, some_region):
>        return or_(
>        Layout.logoRegion==region,
>        Layout.mainImageRegion==region,
>        Layout.backgroundRegion==region
>        )
>
> s.query(Layout).filter(Layout.has_region(some_region))
>
>
>
> --
> 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.
>

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