[英]How do you add a child node to a parent in an adjacency list in sqlalchemy?
I was looking at the example below. 我在看下面的例子。 My program is setup similar but without the backref.
我的程序设置类似,但是没有backref。 I want a one-to-many relationship.
我想要一对多的关系。 Children need not know their parents.
孩子们不需要认识父母。 I am just a but confused as the line "node = TreeNode('rootnode')", wouldn't that be ran everytime the program is executed and possibly create duplicates?
我只是一句,但很困惑,因为“ node = TreeNode('rootnode')”行是否会在每次执行程序时都运行,并且可能会创建重复项?
How do you add a subnode without the parent backref? 如何添加没有父backref的子节点?
I see for example the following 我看到例如以下
node = TreeNode('rootnode')
TreeNode('node1', parent=node)
node1 is created with name 'node1' and it's parent is passed alone. 创建的node1名称为“ node1”,其父级单独传递。 parent is defined in the children relationship as
父级在子级关系中定义为
backref("parent", remote_side=id)
I don't much need children to know parents, just parents knowing children. 我不需要孩子认识父母,只需要父母认识孩子。 How do you create the relationship so parents know children?
您如何建立这种关系,使父母认识孩子?
http://docs.sqlalchemy.org/en/latest/_modules/examples/adjacency_list/adjacency_list.html http://docs.sqlalchemy.org/en/latest/_modules/examples/adjacency_list/adjacency_list.html
rootnode = TreeNode('rootnode')
# assign "children" attribute straight away:
node1 = TreeNode('node1',
children=[TreeNode('node1.child1'), TreeNode('node1.child2')]
)
# or just manipulate as any regular list
rootnode.children.append(node1)
I the example you link to parent
already knows children
by the fact that the relationship
children is defined: 在您链接到
parent
的示例中,通过定义子级relationship
这一事实已经知道了children
:
children = relationship("TreeNode", # NOTE: now each TreeNode has "children"
# ...
backref=backref("parent", remote_side=id), # this line allows children to link to "parent"
# ...
)
Therefore 因此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.