13

私は次のようなsqlalchemyで自己参照の多対多の関係(Lineは多くの親行と多くの子行を持つことができることを意味します)を作成しようとしています:

Base = declarative_base()

class Association(Base):
 __tablename__ = 'association'

 prev_id = Column(Integer, ForeignKey('line.id'), primary_key=True)                            
 next_id = Column(Integer, ForeignKey('line.id'), primary_key=True)


class Line(Base):
 __tablename__ = 'line'

 id = Column(Integer, primary_key = True)
 text = Column(Text)
 condition = Column(Text)
 action = Column(Text)

 next_lines = relationship(Association, backref="prev_lines")



class Root(Base):
 __tablename__ = 'root'

 name = Column(String, primary_key = True)
 start_line_id = Column(Integer, ForeignKey('line.id'))

 start_line = relationship('Line')

しかし、次のエラーが発生します:sqlalchemy.exc.ArgumentError:リレーションシップLine.next_linesの親/子テーブル間の結合条件を判別できませんでした。'primaryjoin'式を指定します。'secondary'が存在する場合は、'secondaryjoin'も必要です。

私がこれをどのように改善できるか知っていますか?

4

1 に答える 1

7

必要なのは次のとおりです。

prev_lines = relationship(
    Association,
    backref="next_lines",
    primaryjoin=id==Association.prev_id)

これは後方参照を指定するため、関係next_linesを持つ必要はありません。next_lines

remote_side関係へのパラメーターを使用してこれを行うこともできます: http ://www.sqlalchemy.org/trac/browser/examples/adjacency_list/adjacency_list.py

于 2010-11-17T15:06:05.770 に答える