小编典典

ArgumentError:关系需要类或映射器参数

python

我遇到了这个奇怪的错误,我说的很奇怪,因为我对一个不相关的表进行了更改。

我正在尝试查询如下tDevice所示的表:

class TDevice(Base):
    __tablename__ = 'tDevice'

    ixDevice = Column(Integer, primary_key=True)
    ixDeviceType = Column(Integer, ForeignKey('tDeviceType.ixDeviceType'), nullable=False)
    ixSubStation = Column(Integer, ForeignKey('tSubStation.ixSubStation'), nullable=False)
    ixModel = Column(Integer, ForeignKey('tModel.ixModel'), nullable=True)
    ixParentDevice = Column(Integer, ForeignKey('tDevice.ixDevice'), nullable=True)
    sDeviceName = Column(Unicode(255), nullable=False)#added

    children = relationship('TDevice',
                        backref=backref('parent', remote_side=[ixDevice]))

    device_type = relationship('TDeviceType',
                           backref=backref('devices'))

    model = relationship('TModel',
                     backref=backref('devices'))

    sub_station = relationship('TSubStation',
                           backref=backref('devices'))

这就是我的查询方式:

Device = DBSession.query(TDevice).filter(TDevice.ixDevice == device_id).one()

一旦执行此行,我就会收到错误:

ArgumentError: relationship 'report_type' expects a class or a mapper argument (received: <class 'sqlalchemy.sql.schema.Table'>)

我所做的唯一更改是在tReportTable 其中添加了一个report_type关系,现在看起来像这样:

class TReport(Base):
__tablename__ = 'tReport'

ixReport = Column(Integer, primary_key=True)
ixDevice = Column(Integer, ForeignKey('tDevice.ixDevice'), nullable=False)
ixJob = Column(Integer, ForeignKey('tJob.ixJob'), nullable=False)
ixReportType = Column(Integer, ForeignKey('tReportType.ixReportType'), nullable=False) # added

report_type = relationship('tReportType',
                           uselist=False,
                           backref=backref('report'))

device = relationship('TDevice',
                      uselist=False,
                      backref=backref('report'))

job = relationship('TJob',
                   uselist=False,
                   backref=backref('report'))

我还是SqlAlchemy的新手,所以我似乎看不到如果我在迭代另一个表时如何添加该关系会导致此错误。


阅读 220

收藏
2020-12-20

共1个答案

小编典典

我不高兴自己,因为这是一个愚蠢的错误,但这是我的罪魁祸首:

report_type = relationship('tReportType',
                           uselist=False,
                           backref=backref('report'))

应该:

report_type = relationship('TReportType',
                           uselist=False,
                           backref=backref('report'))

大写的T而不是t,我应该引用的是类,而不是我的实际表名:'tReportType'->'TReportType'

2020-12-20