ArgumentError:relationship需要一个类或映射器参数

时间:2014-07-28 19:15:29

标签: python sqlalchemy pyramid

我收到了这个奇怪的错误,我说的很奇怪,因为我改变了一张不相关的表格。

我正在尝试查询我的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的新手,所以如果我在迭代另一个表,我似乎无法看到如何添加该关系会导致此错误

1 个答案:

答案 0 :(得分:40)

对自己并不满意,因为这是一个如此愚蠢的错误,但这是我的罪魁祸首:

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

应该是:

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

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