Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy列_属性基础_Python_Orm_Sqlalchemy_Declarative - Fatal编程技术网

Python SQLAlchemy列_属性基础

Python SQLAlchemy列_属性基础,python,orm,sqlalchemy,declarative,Python,Orm,Sqlalchemy,Declarative,我有两种型号: 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(Rep

我有两种型号:

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中是否有任何记录。我试着用这种方式:

class Report(Base):
    __tablename__ = 'report'
    id = Column(Integer, primary_key=True)

    has_photo = column_property(
        select(ReportPhoto.any())
    )

但获取一个错误
NameError:未定义名称“ReportPhoto”
。如何解决此问题?

类似的方法应该可以:

    class ReportPhoto(Base):
        __tablename__ = 'report_photo'
        id = Column(Integer, primary_key=True)
        report_id = Column(Integer, ForeignKey('report.id'), nullable=False)

    class Report(Base):
        __tablename__ = 'report'
        id = Column(Integer, primary_key=True)
        report_photos = relationship(ReportPhoto, backref='report')
        has_photo = column_property(
            exists().where(ReportPhoto.report_id==id)
        )

我将补充@Vladimir lliev的回答,并向其他可能不知道如何做到这一点的人澄清

将具有“外部表引用”列_属性的表放在其引用的列之后。在这种情况下,它意味着将报告放在ReportPhoto之后。这将解决您的NameError,但是,ReportPhoto外键引用上会留下一个新错误。要解决此问题,请将外键表引用放在引号中。您可以通过引用声明性文档(例如declarative.py)并查看“配置关系”下的内容来阅读更多内容——具体来说,请阅读有关引用外部引用的部分

使用您的代码,这看起来像:

class ReportPhoto(Base):
    # This now goes first
    __tablename__ = 'report_photo'
    id = Column(Integer, primary_key=True)
    # Notice the quotations around Report references here
    report_id = Column(Integer, ForeignKey("Report.id"), nullable=False)

    # Notice the quotations around Report references here
    report = relationship("Report", 
           uselist=False, 
           backref=backref("report_photo", uselist=True))

class Report(Base):
    # This is now _after_ ReportPhoto
    __tablename__ = 'report'
    id = Column(Integer, primary_key=True)

    # ReportPhoto now exists and we will not trip a NameError exception
    has_photo = column_property(
        select(ReportPhoto.any())
    )

column\u属性
是访问连接的
ReportPhoto
记录,还是返回随机的
ReportPhoto
记录?