[英]Flask SQLAlchemy Adjacency List Relationships¶
I am trying to set up a table that should include a foreign key that points to the table itself just like a tree with nodes and therefore a parent and children.我正在尝试建立一个表,该表应包含一个指向表本身的外键,就像一棵带有节点的树,因此有父节点和子节点。
I followed the example on https://docs.sqlalchemy.org/en/14/orm/self_referential.html which looks like the following我按照https://docs.sqlalchemy.org/en/14/orm/self_referential.html上的示例进行操作,如下所示
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('node.id'))
data = Column(String(50))
children = relationship("Node")
When implementing this class in Flask I get the following error:在 Flask 中实现此类时出现以下错误:
The foreign key associated with column 'node.parent_id' could not find table 'node' with which to generate a foreign key to target column 'id'.与列“node.parent_id”相关联的外键找不到表“node”,用于生成目标列“id”的外键。
Any ideas on how to fix it?关于如何解决它的任何想法?
Thanks in advance提前致谢
My understanding is the relationship("something") looks for the class with tablename = "something" (not the class name).我的理解是 relationship("something") 寻找具有tablename = "something"(不是类名)的类。 See if changing it to children = relationship("node") works
查看将其更改为 children = relationship("node") 是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.