0

我使用 SQLAlchemy 来表示作者之间的关系。我希望作者与其他作者(coauthorshp)相关,并在关系中有额外的数据,这样a我就可以找到他们的合著者。

这是如何在两个不同的对象之间完成的:

class Association(Base):
    __tablename__ = 'association'
    left_id = Column(Integer, ForeignKey('left.id'), primary_key=True)
    right_id = Column(Integer, ForeignKey('right.id'), primary_key=True)
    extra_data = Column(String(80))
    child = relationship('Child', backref='parent_assocs')

class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship('Association', backref='parent')

class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)

但在我的情况下我该怎么做呢?

共同作者的性质是双向的。那么,当您(id_left, id_right)通过 coauthoship 对象将元组插入到 coauthorship 表中时,有没有一种方法可以轻松地插入反向关系?我问是因为我想使用关联代理。

4

1 回答 1

0

if you'd like to literally have pairs of rows in association, that is, for every id_left, id_right that's inserted, you also insert an id_right, id_left, you'd use an attribute event to listen for append events on either side, and produce an append in the other direction.

If you just want to be able to navigate between Parent/Child in either direction, just a single row of id_left, id_right is sufficient. The examples in the docs regarding this kind of mapping illustrate the whole thing.

于 2013-06-03T23:57:57.920 回答