如何更正此sqlalchemy.exc.NoForeignKeysError?

Dan*_*ite 10 python sqlalchemy

为什么我会得到TraceBack __CODE__

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.
Run Code Online (Sandbox Code Playgroud)

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

当在立法者阶级中明确定义一个外键关系时,为什么会抱怨缺少外键关系?

小智 13

AFAIK你应该在ForeignKey中使用tablename:

CountyCode = Column('CountyCode', String, ForeignKey('tblCounty.CountyCode'))
Run Code Online (Sandbox Code Playgroud)

  • 或者,为了减少混淆,当你可以避免它时不要使用字符串:`ForeignKey(County.CountyCode)` (6认同)