python – SQLAlchemy中的column_property上的延迟加载

前端之家收集整理的这篇文章主要介绍了python – SQLAlchemy中的column_property上的延迟加载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

说我有以下型号:

class Department(Base):
    __tablename__ = 'departments'
    id = Column(Integer,primary_key=True)

class Employee(Base):
    __tablename__ = 'employees'
    id = Column(Integer,primary_key=True)
    department_id = Column(None,ForeignKey(Department.id),nullable=False)
    department = relationship(Department,backref=backref('employees'))

有时,当我查询部门时,我还想获取他们拥有的员工数量.我可以使用column_property实现这一点,如下所示:

Department.employee_count = column_property(
    select([func.count(Employee.id)])
        .where(Employee.department_id == Department.id)
        .correlate_except(Employee))

Department.query.get(1).employee_count # Works

但是,即使我不需要,也总是通过子查询获取计数.显然我不能要求sqlAlchemy在查询时不加载它:

Department.query.options(noload(Department.employee_count)).all()
# Exception: can't locate strategy for sqlalchemy.orm.properties.ColumnProperty'> (('lazy','noload'),)

我也尝试使用混合属性而不是列属性来实现它:

class Department(Base):
    #...

    @hybrid_property
    def employee_count(self):
        return len(self.employees)

    @employee_count.expression
    def employee_count(cls):
        return (
            select([func.count(Employee.id)])
                .where(Employee.department_id == cls.id)
                .correlate_except(Employee))

没有运气:

Department.query.options(joinedload('employee_count')).all()
# AttributeError: 'Select' object has no attribute 'property'

我知道我可以在查询查询计数作为一个单独的实体,但我真的更喜欢将它作为模型的属性.这在sqlAlchemy中甚至可能吗?

编辑:为了澄清,我想避免N 1问题并将员工数量加载到与部门相同的查询中,而不是在每个部门的单独查询中.

最佳答案
您尝试的加载策略用于关系. column_property的加载方式与普通列的更改方式相同,请参见Deferred Column Loading.

您可以通过将deferred = True传递给column_property来默认延迟加载employee_count.延迟列时,访问属性时会发出select语句.

sqlalchemy.orm中的defer和undefer允许在构造查询时更改它:

from sqlalchemy.orm import undefer
Department.query.options(undefer('employee_count')).all()

猜你在找的Python相关文章