One to one self relationship in SQLAlchemy

前端 未结 1 349
你的背包
你的背包 2021-01-13 00:13

I need to create an SQLAlchemy version of a linked list. It\'s actually more complex than that, but it boils down to this:

I need a one-to-one, self-referential,

1条回答
  •  走了就别回头了
    2021-01-13 00:46

    As exception says you need to set remote_side keyword for relationship. Otherwise sqlalchemy cannot select reference direction.

    class Node(declarative_base()):
        ...
        prev = relationship(
            'Node',
            uselist=False,
            remote_side=[id],
            backref=backref('next', uselist=False)
        )
    

    0 讨论(0)
提交回复
热议问题