有两个类,我都使用,我想查询以下内容:
虚拟类如下:
Base = declarative_base()
class BaseClass(Base):
__tablename__ = 'base'
id = Column(Integer, primary_key=True)
value = Column(String(256), nullable=False)
type = Column(String(40))
__mapper_args__ = {
'polymorphic_identity': 'base',
'polymorphic_on': type
}
class MyClass(BaseClass):
__tablename__ = 'mine'
id = Column(Integer, ForeignKey('qg.id'), nullable=False,
primary_key=True)
data = Column(Integer, nullable=False)
__mapper_args__ = {
'polymorphic_identity': 'mine'
}
感谢任何提示(最好是sqlalchemy,mysql查询也没关系。)
答案 0 :(得分:0)
帮助我的解决方案:
使用mySQL
SELECT value FROM base LEFT JOIN mine ON mine.id = base.id WHERE base.type = 'base' OR mine.data = 1;
或使用sqlalchemy
q = session.query(Base)
q = q.outerjoin(Mine)
q = q.filter(
or_(Base.type == 'Base',
Mine.id == 1))
result = q.all()