簡體   English   中英

如何在sqlalchemy的鄰接表中將子節點添加到父節點?

[英]How do you add a child node to a parent in an adjacency list in sqlalchemy?

我在看下面的例子。 我的程序設置類似,但是沒有backref。 我想要一對多的關系。 孩子們不需要認識父母。 我只是一句,但很困惑,因為“ node = TreeNode('rootnode')”行是否會在每次執行程序時都運行,並且可能會創建重復項?

如何添加沒有父backref的子節點?

我看到例如以下

node = TreeNode('rootnode')
TreeNode('node1', parent=node)

創建的node1名稱為“ node1”,其父級單獨傳遞。 父級在子級關系中定義為

backref("parent", remote_side=id)

我不需要孩子認識父母,只需要父母認識孩子。 您如何建立這種關系,使父母認識孩子?

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)

在您鏈接到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"
                    # ...
                )

因此

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM