如何更正此sqlalchemy.exc.NoForeignKeysError?

时间:2015-01-18 22:25:05

标签: python sqlalchemy

为什么我会收到TraceBack

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition
between parent/child tables on relationship County.Legislators - 
there are no foreign keys linking these tables.

Ensure that referencing columns are associated with a
ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

使用以下型号:

class County(Base):
    __tablename__ = 'tblCounty'
    CountyCode = Column('CountyCode', String, primary_key=True)
    Legislators = relationship('Legislators', backref='County', lazy='dynamic')

class Legislators(Base):
    __tablename__ = 'VLegislators'
    EmployeeNo = Column('EmployeeNo', String, primary_key=True)
    CountyCode = Column('CountyCode', String, ForeignKey('County.CountyCode'))

我正在尝试映射新罕布什尔州提供的面向公众的MS SQL数据库。因此,不允许更改架构。

为什么在立法会议员中明确界定一个外国关系时,它是否抱怨缺乏外键关系?

1 个答案:

答案 0 :(得分:13)

AFAIK你应该在外键中使用表名:

CountyCode = Column('CountyCode', String, ForeignKey('tblCounty.CountyCode'))