On May 3, 2012, at 7:00 PM, Wu-bin Zhen wrote:
OK that test illustrates what's going on, thanks for that.
>
> >
> > As you see, there are two "AS id" in the statement and causing the
> > error. I was wondering why it asks "resource.id" which I didn't
> > specify, and it would know that BasePrAc.id is equal to Resource.id
> > since BasePrAc class is the subclass of Resource class. Some suggested
> > to use "alias" for joining, and I tried but it didn't work. Besides, I
> > am hoping not to use alias everytime, so joining things could be more
> > generic.
It's true, the column loader here is actually aware of both columns at the same
time. It's very easy to make it render one or the other and no others, such
as via a patch like this:
diff -r 572d4ebbca4b lib/sqlalchemy/orm/strategies.py
--- a/lib/sqlalchemy/orm/strategies.py Sun Apr 29 18:53:29 2012 -0400
+++ b/lib/sqlalchemy/orm/strategies.py Thu May 03 19:27:30 2012 -0400
@@ -114,10 +114,11 @@
def setup_query(self, context, entity, path, reduced_path,
adapter, column_collection, **kwargs):
- for c in self.columns:
+ for c in reversed(self.columns):
if adapter:
c = adapter.columns[c]
column_collection.append(c)
+ break
def init_class_attribute(self, mapper):
self.is_class_level = True
However with this, many tests fail. There are lots of situations where columns
from either the parent or child tables are expected.
The "id" column of both tables are considered as different within SQLAlchemy,
and most ORM queries qualify the column names with the tablename so that they
are separately targeted during automated joins and in the result set.
The hierarchy of mappers considers the primary key columns to be the columns
within the base table only, so that polymorphic queries make sense. So the
column from the "base" table needs to be present.
But then there's lots of cases where things refer to the sub-table column.
SQLAlchemy would need to have translation logic at all points to accommodate
this, which becomes very awkward for edge cases like eager loading from a
polymorphic relationship. It also means things like "primaryjoin" conditions
that refer to the non-base column need to be translated explicitly even if
that's not what the user defined.
So this is a very complicated issue. For now, two ways to work around:
1. put the tablename qualification back on:
sq = s.query(BasePrAc).with_labels().subquery()
But then explicit reference to the columns needs to include the tablename:
q = s.query(Post).join((sq, sq.c.base_products_accessories_id ==
Post.basePrAcId)).all()
Though in this case you can just join using the relationship:
q = s.query(Post).join((sq, Post.basePrAc)).all()
2. build a join using fold_equivalents:
This is something that could theoretically make it's way into Query with some
complexity. join() specifically supports "folding" the equivalently named and
FK-linked columns. It's a little tricky to get it into the mapped join but
you can do it by hand:
sq =
BasePrAc.__mapper__.mapped_table.select(fold_equivalents=True).alias()
above, fold_equivalents is only accepted by the select() that comes from a Join
object, which in this case BasePrAc.__mapper__.mapped_table is.
> >
> > I will appreciate if you have any advise for this situation.
> >
> > Thank you very much, and have a nice day.
> > W
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "sqlalchemy" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.