sqlalchemy中是否有办法在单个查询中关闭声明性的多态连接加载?大部分时间都很好,但我有:
class A(Base) :
discriminator = Column('type', mysql.INTEGER(1), index=True, nullable=False)
__mapper_args__ = { 'polymorphic_on' : discriminator }
id = Column(Integer, primary_key=True)
p = Column(Integer)
class B(A) :
__mapper_args__ = { 'polymorphic_identity' : 0 }
id = Column(Integer, primary_key=True)
x = Column(Integer)
class C(A) :
__mapper_args__ = { 'polymorphic_identity' : 1 }
id = Column(Integer, primary_key=True)
y = Column(String)
我想进行查询,以便获得B.x>的所有A.ids。 10,如果A实际上是B,或者C.y =='blah',如果A实际上是C,则全部按p排序。
为了迭代地进行,我只是从第一部分开始 - “如果A实际上是B,则获取所有A.id,其中B.x> 10。”所以我想我会从外部联接开始:
session.query(A.id).outerjoin((B, B.id == A.id)).filter(B.x > 10)
...除了似乎没有办法避免让外连接((B,B.id == A.id))子句生成A中所有内容与子选择中B中所有内容的完全连接。如果B没有从A继承,那么这种情况就不会发生,所以我认为这是多态声明代码生成。有没有办法把它关掉?或者强迫外部连接做我想做的事情?
我想要的是这样的:
select a.id from A a left outer join B b on b.id == a.id where b.x > 10
但我会得到类似的东西:
select a.id from A a left outer join (select B.id, B.x, A.id from B inner join A on B.id == A.id)
......顺便说一句,如果不可能,那么后者效率会低于前者吗? sql引擎实际上是执行内连接,还是会忽略它?
答案 0 :(得分:1)
你应该使用with_polymorphic()而不是outerjoin(),这似乎会返回预期的结果:
session.query(A).with_polymorphic(B).filter(B.x > 10).all()
# BEGIN
# SELECT "A".type AS "A_type", "A".id AS "A_id", "A".p AS "A_p", "B".id AS "B_id", "B".x AS "B_x"
# FROM "A" LEFT OUTER JOIN "B" ON "A".id = "B".id
# WHERE "B".x > ?
# (10,)
# Col ('A_type', 'A_id', 'A_p', 'B_id', 'B_x')
与:相比:
session.query(A.id).outerjoin((B, B.id == A.id)).filter(B.x > 10)
# BEGIN
# SELECT "A".id AS "A_id"
# FROM "A" LEFT OUTER JOIN (SELECT "A".type AS "A_type", "A".id AS "A_id", "A".p AS "A_p", "B".id AS "B_id", "B".x AS "B_x"
# FROM "A" JOIN "B" ON "A".id = "B".id) AS anon_1 ON anon_1."A_id" = "A".id
# WHERE anon_1."B_x" > ?
# (10,)
# Col ('A_id',)
我用来测试这个代码,以防任何人想要测试SQLAlchemy这个简洁的位:
#!/usr/bin/env python
import logging
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class A(Base) :
__mapper_args__ = { 'polymorphic_on' : discriminator }
__tablename__ = 'A'
id = Column(Integer, primary_key=True)
discriminator = Column('type', Integer, index=True, nullable=False)
p = Column(Integer)
class B(A) :
__mapper_args__ = { 'polymorphic_identity' : 0 }
__tablename__ = 'B'
id = Column(Integer, ForeignKey('A.id'), primary_key=True)
x = Column(Integer)
class C(A) :
__mapper_args__ = { 'polymorphic_identity' : 1 }
__tablename__ = 'C'
id = Column(Integer, ForeignKey('A.id'), primary_key=True)
y = Column(String)
meta = Base.metadata
meta.bind = create_engine('sqlite://')
meta.create_all()
Session = sessionmaker()
Session.configure(bind=meta.bind)
session = Session()
log = logging.getLogger('sqlalchemy')
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
session.query(A.id).outerjoin((B, B.id == A.id)).filter(B.x > 10).all()
session.query(A).with_polymorphic(B).filter(B.x > 10).all()
我使用SQLAlchemy 0.6.4在Python 2.7上运行它。
答案 1 :(得分:1)
您可以尝试单独为每个子类构建查询,然后将它们组合在一起。查询B.id
时,SQLAlchemy隐式加入超类并返回A.id
,因此对B.id
和C.id
的选择联合只返回一列。
>>> b_query = session.query(B.id).filter(B.x > 10)
>>> c_query = session.query(C.id).filter(C.y == 'foo')
>>> print b_query.union(c_query)
SELECT anon_1."A_id" AS "anon_1_A_id"
FROM (SELECT "A".id AS "A_id"
FROM "A" JOIN "B" ON "A".id = "B".id
WHERE "B".x > ? UNION SELECT "A".id AS "A_id"
FROM "A" JOIN "C" ON "A".id = "C".id
WHERE "C".y = ?) AS anon_1
您仍然可以获得一个子选择,但只有一个“连接层” - 外部选择只是重命名该列。