且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sqlalchemy中的多对多自引用关系

更新时间:2023-01-14 23:20:08

您应该只需要:

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

由于它指定了next_lines后向引用,因此无需具有next_lines关系.

Since this specifies the next_lines back reference there is no need to have a next_lines relationship.

您也可以使用remote_side参数来建立关系:

You can also do this using the remote_side parameter to a relationship: http://www.sqlalchemy.org/trac/browser/examples/adjacency_list/adjacency_list.py