我有两个模型:
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))
我想在报告模型中添加列,表明 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
。我该如何解决这个问题?