On Oct 12, 2013, at 10:26 AM, Jerry <jerryji1...@gmail.com> wrote:

> SQLAlchemy 0.8 is generating wrong SQL for my simple self-join --
> 
> class ScheduledJob(Base):
>     Id = Column('Id', Integer, primary_key=True)
>     DependentJob1 = Column('DependentJob1', Integer)
> 
> DJ1 = aliased(ScheduledJob)
> query = dbsession.query(ScheduledJob.Id, DJ1.Id).outerjoin(
>         DJ1, ScheduledJob.DependentJob1==DJ1.Id)
> print query
> 
> prints the wrong SQL --
> 
> SELECT "ScheduledJob"."Id" AS "ScheduledJob_Id"
> FROM "ScheduledJob" LEFT OUTER JOIN "ScheduledJob" AS "ScheduledJob_1" ON
> "ScheduledJob"."DependentJob1" = "ScheduledJob"."Id"

cannot reproduce.  test case run on 0.8.2, 0.8.1, 0.8.0, results are correct:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class ScheduledJob(Base):
    __tablename__ = 'ScheduledJob'

    Id = Column('Id', Integer, primary_key=True)
    DependentJob1 = Column('DependentJob1', Integer)

session = Session()

DJ1 = aliased(ScheduledJob)
query = session.query(ScheduledJob.Id, DJ1.Id).outerjoin(DJ1, 
ScheduledJob.DependentJob1==DJ1.Id)
print query


output:

SELECT "ScheduledJob"."Id" AS "ScheduledJob_Id", "ScheduledJob_1"."Id" AS 
"ScheduledJob_1_Id" 
FROM "ScheduledJob" LEFT OUTER JOIN "ScheduledJob" AS "ScheduledJob_1" ON 
"ScheduledJob"."DependentJob1" = "ScheduledJob_1"."Id"

I suspect that in your actual application, "DJ1" is not actually an alias of 
ScheduledJob.

> BTW, I'm mapping multiple databases, could it be related to this problem?

not really, no.    


> Sorry this is a "repost" of my SO question (which has not seen an answer for 
> two days) 
> http://stackoverflow.com/questions/19315202/sqlalchemy-generates-wrong-sql-for-aliased-and-self-join

copied this response to there.


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to