sqlalchemy.exc.NoForeignKeysError:无法确定父/子表之间的联接条件

2024-06-16 11:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有好几个班都有同样的关系。例如:

class Foo(db.Model):
    __tablename__ = 'foo'
    foonr = Column('foonr', Integer, primary_key=True)
    bar= Column('bar_code',String, ForeignKey('bar.bar_code'))
    bar_rel = relationship('Foo', back_populates='foo_rel')

class Bar(db.Model):
    __tablename__ = 'bar'
    code = Column('bar_code', String, primary_key=True)
    foo_rel = relationship('Foo', uselist=False, back_populates=' bar_rel')

这几乎是来自:http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html的复制粘贴。但是,在这个例子中,当我试图查询一个无关的类(不是Foo或Bar)时,我得到了以下错误:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship ... - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

有人能解释一下这种行为吗?帮我解决这个问题?在

谢谢。在


Tags: keytruedbmodelfoobarcodecolumn