On Oct 9, 2014, at 6:45 PM, Aurélien C. <aurelien.s.qlalch...@ap2c.com> wrote:

> Hi, 
> 
> I have a class which represents a table with polymorphic enabled. When I 
> directly access to this table the polymorphic mechanism adds the needed 
> condition and everything works perfectly. However when this table is joined 
> to a request the condition is missing...
> 
> I attached a quick sample to this email.
> My output:
> 
> 2014-10-10 00:18:44,582 INFO sqlalchemy.engine.base.Engine SELECT 
> "myTable"."otherId" AS "myTable_otherId" 
> FROM "myTable" 
> WHERE "myTable".id = ? AND "myTable".flag IN (?)
> 2014-10-10 00:18:44,582 INFO sqlalchemy.engine.base.Engine (1, 1)
> A contains 2
> A contains 3
> 2014-10-10 00:18:44,583 INFO sqlalchemy.engine.base.Engine SELECT 
> "otherTable".id AS "otherTable_id", "myTable"."otherId" AS "myTable_otherId" 
> FROM "otherTable" JOIN "myTable" ON "myTable".id = "otherTable".id
> 2014-10-10 00:18:44,583 INFO sqlalchemy.engine.base.Engine ()
> Now A contains 2
> Now A contains 3
> Now A contains 4
> Now A contains 5
> 
> 
> The last lines are not part of the A class due to the polymorphic 
> constraint... However they are present since the polymorphic condition is 
> missing.
> 
> Maybe I missed something?

this is a problem because it is very unlikely a user will know how to handle 
this correctly, as even I wasn't sure at first...but I think it can be resolved 
only in 1.0 - see 
https://bitbucket.org/zzzeek/sqlalchemy/issue/3222/join-to-single-inh-subclass-without-using.
  (it's resolved).

SQLAlchemy always treats join() and outerjoin() the same way, it can't be 
inconsistent in those.   So we can't just add a WHERE clause here, because if 
it were an outer join, the query would be wrong.

So the extra discriminator for "mytable.flag IN (?)" has to be in the ON 
clause.  Which it is, if you use a relationship() to create this join:

        class OtherTable(Base):
                # ...

                a = relationship("_motherClass")

        query(OtherTable.id, A.otherID).join(A, OtherTable.a)

but as it is, you are hand-rolling the ON clause yourself.  So the ORM doesn't 
get involved.   In #3222, I'll have it get involved, but that's going to 
surprise folks so it is for 1.0.  In 0.9, you'd need to roll it like this:

        query(OtherTable.id, A.otherID).join(A, and_(OtherTable.id == A.id, 
A.flag == True))




-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to