>
> rating = aliased(BuildingRating)
> care_level = aliased(BuildingCareLevel)
>
> to make later:
>
> .....query(Building).filter( or_( rating.value.like("%good"),
> care_level.value.like("well_groomed") ) )
>
> But I couldn't work out how to explicit name the aliased classes or
> get the aliased name. If I could do something like:
>
> rating = aliased(BuildingRating)
> care_level = aliased(BuildingCareLevel)
>
> query = session.query(Building).join('condition',
> rating).join('condition', care_level)
> query = query.filter( or_( care_level.value.like('well-groomed'),
> rating.value.like('%good') ) )
this is nearly a valid query. join is called as in join(rating, Building.condition), join(care_level, Building.condition). Assuming BuildingRating and BuildingCareLevel are both single table off of whatever Building.condition is it should be straightforward.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/Z-JEIbJEInkJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> Here is my mapping, this time in german...
OK, sifting through lots of extraneous details as well as the lack of the actual table definitions, it seems like you're looking to join from Building->BuildingCondition->Lookup.
I don't use aliased=True very often, but I think its usage pattern would allow this:
lc1 = aliased(LookupSubclassOne)
lc2 = aliased(LookupSubclassTwo)
query(Building).\
join(Building.condition,aliased=True).\
join(lc1, BuildingCondition.rating, from_joinpoint=True).\
join(Building.condition, aliased=True).\
join(lc2, BuildingCondition.care_level, from_joinpoint=True).\
filter(...)
I don't talk about aliased=True often because it has a specific effect on subsequent modifications to the query, which are then reset on the next call to join(), and its a little confusing/hard to explain. In this case, the second and fourth calls to join() add from_joinpoint=True so that it goes from the previous joinpoint.
For a full explicit approach, just alias everything:
bc1 = aliased(BuildingCondition)
bc2 = aliased(BuildingCondition)
lc1 = aliased(LookupSubclassOne)
lc2 = aliased(LookupSubclassTwo)
query(Building).\
join(bc1, Building.condition).\
join(lc1, bc1.rating).\
join(bc2, Building.condition).\
join(lc2, bc2.care_level).\
filter(...)
note I'm using the 0.7 style of joins here where you can say join(target, onclause) without an embedded tuple.
> Hello Michael,
>
> thanks a lot again, sorry for not extracting the right parts...I'll
> try to do that better next time.
>
>
>> bc1 = aliased(BuildingCondition)
>> bc2 = aliased(BuildingCondition)
>> lc1 = aliased(LookupSubclassOne)
>> lc2 = aliased(LookupSubclassTwo)
>>
>> query(Building).\
>> join(bc1, Building.condition).\
>> join(lc1, bc1.rating).\
>> join(bc2, Building.condition).\
>> join(lc2, bc2.care_level).\
>> filter(...)
>
> (with tuples on 0.6.8) works!!! Yippieh!
>
> I know, i ask much...but I have two remaining questions:
>
> 1. Why do I have to make two joins on a table, which I need only once?
> Two joins to Building.condition, although I need only aliased
> versiones of the lookup table and in sql I need only one join
> to Building.condition
ah good point. yes you only need BuildingCondition once really
query(Building).join(Building.condition).join(lc1, Building.rating).join(lc2, Building.care_level)
should work too.
>
> 2. The workinig method produces two times the polymorphic_identity on
> the second join:
> It looks like this:
it seems like you have "lookup_category_id=6" in your primary join conditions. relationship() when presented with A->C, where C is a single table subclass of B, will always stick the " discriminator IN (C, <descendants of C>)" on the end of a JOIN condition.