sqlalchemy-如何在同一表中设置与back_population to popupulation
我尝试设置 parent_id 包含一个人的 id 以 PEOPUR 表中的 id 以的方式进行 id ,以ORM的方式进行 ,并在它们之间进行了背部填充,但行不通。
class People(Base):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
name = Column(String(20), nullable=False, unique=True)
parent_id = Column(Integer, ForeignKey('people.id'))
parent = relationship("People", back_populates="parent", uselist=False)
engine = create_engine(
f'mssql://{username}:{password}@{server_name}/{db_name}?driver=SQL+Server&trusted_connection=yes')
Session = sessionmaker(bind=engine)
session = Session()
session.add(People(name='me'))
提高sa_exc.argumenterror( sqlalchemy.exc.argumenterror:people.parent和back-reference people.parent都是相同的方向符号('Onetomany')。您的意思是将远程_SEDE设置在多对一方面吗?
I try to set fk which parent_id contains id of a person in People table in orm manner and backpopulate between them but it does not work.
class People(Base):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
name = Column(String(20), nullable=False, unique=True)
parent_id = Column(Integer, ForeignKey('people.id'))
parent = relationship("People", back_populates="parent", uselist=False)
engine = create_engine(
f'mssql://{username}:{password}@{server_name}/{db_name}?driver=SQL+Server&trusted_connection=yes')
Session = sessionmaker(bind=engine)
session = Session()
session.add(People(name='me'))
raise sa_exc.ArgumentError(
sqlalchemy.exc.ArgumentError: People.parent and back-reference People.parent are both of the same direction symbol('ONETOMANY'). Did you mean to set remote_side on the many-to-one side ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
远程_SEDE
参数。这是我使用的代码适应您的示例:
You can use the
remote_side
argument.Here's code I'm using adapted to your example: