说我有以下型号:
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中甚至可能吗?
最佳答案
您尝试的加载策略用于关系. column_property的加载方式与普通列的更改方式相同,请参见Deferred Column Loading.
原文链接:https://www.f2er.com/python/438622.html您可以通过将deferred = True传递给column_property来默认延迟加载employee_count.延迟列时,访问属性时会发出select语句.
sqlalchemy.orm中的defer和undefer允许在构造查询时更改它:
from sqlalchemy.orm import undefer
Department.query.options(undefer('employee_count')).all()