2013-05-19 26 views
0

我使用SQLAlchemy来表示作者之间的关系。我希望有与其他作者相关的作者(coauthorshp),并且在关系中有额外的数据,因此通过作者a我可以找到他们的合着者。从表到SQLAlchemy自身的多对多关系

如何,这是两个不同的对象之间进行的是:

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) 

,但我会怎么做这在我的情况?

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

+0

你能举一个你究竟想要做什么的例子吗?阅读你的问题是有道理的,但需要更多的信息。 – CodeLikeBeaker

回答

0

,如果你想从字面上对中行association,也就是说,每一个id_left, id_right会插入,也插入id_right, id_left,你会使用一个attribute event监听两侧追加事件,并产生在另一个方向追加。

如果您只是希望能够在任何方向上在父/子之间导航,只需要一行id_left, id_right就足够了。文档中关于这种映射的例子说明了整个事情。

相关问题