我有两个模型:
class Report(Base):
__tablename__ = 'report'
id = Column(Integer, primary_key=True)
class ReportPhoto(Base):
__tablename__ = 'report_photo'
id = Column(Integer, primary_key=True)
report_id = Column(Integer, ForeignKey(Report.id), nullable=False)
report = relationship(Report, uselist=False, backref=backref('report_photo', uselist=True))
我希望给Report模型添加一个列,用于指示ReportPhoto中是否有任何记录。我尝试使用column_property这种方式:
class Report(Base):
__tablename__ = 'report'
id = Column(Integer, primary_key=True)
has_photo = column_property(
select(ReportPhoto.any())
)
但是收到一个错误 NameError: name 'ReportPhoto' is not defined
。我该如何解决这个问题?
column_property
是访问已连接的ReportPhoto
记录还是返回随机的ReportPhoto
记录? - ChaimG