2017-06-23 44 views
0

我有幾個具有相同關係的類。例如:sqlalchemy.exc.NoForeignKeysError:無法確定父/子表之間的連接條件

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複製粘貼。然而,當我試圖在這個例子來查詢一個不相關的類(不富或酒吧),我得到以下錯誤:

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.

有人能解釋這種現象?幫我解決這個問題?

謝謝。

回答

1

Foorelationship應該是:

bar_rel = relationship('Bar', back_populates='foo_rel')