私は関係モデルでイベントリスナーを使用しての問題を抱えている、私のモデルクラスは、自己参照するテーブルである:SQLAlchemyのイベントリスナーと関係の問題
class Distributor(Base):
__tablename__ = "distributors"
id = Column(Integer, primary_key=True)
name = Column(String, nullable = False)
upline_id = Column(Integer, ForeignKey('distributors.id'))
upline = relationship('Distributor', remote_side=id, backref=backref('downlines'))
と私はイベントにリスナーを登録するトリングよダウンラインのコレクションに追加する:
def my_append_listener(target, value, initiator):
branch_develop = len(target.downlines)
と、この行:
event.listen(Distributor.downlines, 'append', my_append_listener)
意志がERROを与えますR:はAttributeError:型オブジェクトのディストリビューターは、「いいえ属性「ダウンライン」
を持っていますが、のような何か書くことokです:
george = Distributor("george", None)
george.downlines = [Distributor("downlineUser")]
を、私はまた私がこれまでの関係を書き換える場合ことがわかった:
downlines = relationship('Distributor', backref=backref('upline', remote_side=id))
すべてが完全に動作します。コード内の何が間違っているか教えてもらえますか?