自己参照のrelation @ sqlalchemy 0.5
自己参照型のrelationを作ろうとしていて、子供を追加しても「循環参照だ!」って怒られてプチ困った。 マニュアル読んだら答かいてあったよメモ
以下、Adjacency List Relationships - Mapper Configuration からコピペ
親 > 子は普通で良い
mapper(Node, nodes, properties={
'children': relation(Node)
})
子 > 親はremote_sideという引数にカラムを指定する
mapper(Node, nodes, properties={
'children': relation(Node, backref=backref('parent', remote_side=[nodes.c.id]))
})
remote_sideは
used for self-referential relationships, indicates the column or list of columns that form the "remote side" of the relationship.
とのこと。これ以上はコードを追ってないので、なんでこうしないと普通の1:Nとして関係が作れないかはわからず…
