对包含派生类的基类的SQLAlchemy查询

时间:2019-08-12 16:44:45

标签: python sqlalchemy

我在sqlalchemy中使用单表继承配置来创建几个派生类。查询基类时,结果还包括所有派生类。我希望结果仅包括基类。

sqlalchemy文档中使用的示例代码。

from sqlalchemy import create_engine, text, func, and_, Column, Integer, String, ForeignKey, Boolean, MetaData, Table
from sqlalchemy.orm import sessionmaker, with_polymorphic, backref, relationship
from sqlalchemy.ext.declarative import declarative_base, declared_attr, has_inherited_table


POSTGRES = {
    'user': 'sqlalchemy',
    'pw': 'sqlalchemy',
    'db': 'sqlalchemy',
    'host': '127.0.0.1',
    'port': '5432',
}

Base = declarative_base()
meta = MetaData()

engine = create_engine('postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES, echo=False)
DBSession = sessionmaker(bind=engine)
s = DBSession()
d = {}


''' Create Base Classes using single table polymorphic identity column '''


class Person(Base):

    __tablename__ = 'people'

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    discriminator = Column('type', String(50))

    __mapper_args__ = {'polymorphic_on': discriminator, 'polymorphic_identity': 'person'}


class Engineer(Person):

    primary_language = Column(String(50))

    __mapper_args__ = {'polymorphic_identity': 'engineer'}


''' Create Empty Schema '''

Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)

''' Create a bunch of objects '''

for id in range(1, 11):

    s.add(Person(name='person_%s' % str(id)))
    s.add(Engineer(name='engineer_%s' % str(id)))
    s.commit()
    s.flush()

print('people: %s' % s.query(Person).count())
print('engineers: %s' % s.query(Engineer).count())

''' Person query should only have 10 results but has 20. Engineer query has 10 results. '''

s.flush()
s.close()

exit()

人员查询应该有10个结果,但还包括工程师。

1 个答案:

答案 0 :(得分:0)

似乎唯一的解决方案是向查询添加过滤器。

people = s.query(Person).filter_by(discriminator='person').all()