SQLAlchemy 0.8 为以下简单的自连接生成错误的 SQL——
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
并打印错误的 SQL——
SELECT "ScheduledJob"."Id" AS "ScheduledJob_Id"
FROM "ScheduledJob" LEFT OUTER JOIN "ScheduledJob" AS "ScheduledJob_1" ON
"ScheduledJob"."DependentJob1" = "ScheduledJob"."Id"
虽然它应该是——
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"
更令人费解的是,我在 SQLAlchemy 文档之后尝试的所有独立别名/连接/自连接测试都可以正常工作。
可能出了什么问题?任何指针将不胜感激。
顺便说一句,我正在映射多个数据库,这可能与这个问题有关吗?