sqlalchemy:多个关系(通过关联对象多对多)

sqlalchemy:多个关系(通过关联对象多对多),sqlalchemy,Sqlalchemy,我正在尝试这样做: class Foo(Base): id = Column(Integer, primary_key=True) class Bar(Foo): id = Column(Integer, primary_key=True) class FooBarAssociation(Base): foo_id = Column(Integer, ForeignKey('foo_table.id')) bar_id = Column(Integer, Fo

我正在尝试这样做:

class Foo(Base):
    id = Column(Integer, primary_key=True)

class Bar(Foo):
    id = Column(Integer, primary_key=True)

class FooBarAssociation(Base):
    foo_id = Column(Integer, ForeignKey('foo_table.id'))
    bar_id = Column(Integer, ForeignKey('bar_table.id'))

    foo = relationship(Foo, backref=...)
    bar = relationship(Bar, backref=...)
…但我会遇到这样的错误:

Could not determine join condition between parent/child tables on relationship FooBarAssociation.foo.  Specify a 'primaryjoin' expression.  If this is a many-to-many relationship, 'secondaryjoin' is needed as well.
我曾尝试在关系声明中指定外键和主连接,但都是徒劳的。帮忙?从富那里继承的酒吧是不是在和我捣乱

谢谢

以下操作应该可以正常工作(与错误显示的完全相同:缺少
primaryjoin
):

如您所见,
bar\u id
列上有两个外键。这可能是继承所必需的,或者您可以删除一个。但是如果你不存储除了多对多关系之外的任何其他信息,那么你可以考虑。
class FooBarAssociation(Base):
    foo_id = Column(Integer, ForeignKey('foo_table.id'), primary_key = True, )
    bar_id = Column(Integer, ForeignKey('bar_table.id'), ForeignKey('foo_table.id'), primary_key = True, )

    foo = relationship(Foo, backref="bars", primaryjoin=(foo_id==Foo.id))
    bar = relationship(Bar, backref="foos", primaryjoin=(bar_id==Bar.id))