2016-08-04 127 views
1

我想在py2neo v3中創建兩個不同類型的現有節點之間的關係,只能使用Cypher執行或者是否有一個函數(可能合併)應該這樣做?在Py2neo上理解合併問題

E.g.

from py2neo import Graph, Path, authenticate, GraphObject 
from py2neo import Node, Relationship 
from py2neo.ogm import * 

a = Node("type1",name = "alice") 
graph.create(a) 
b = Node("type2",name = "bob") 
graph.create(b) 

#now I want to make a relationship of these nodes without using a, b or Relationship 
#Hence something like: 
graph.merge(Node("type1",name = "alice"),"FRIENDS_WITH",Node("type2",name = "bob")) 

的一點是,如果Alice有很多很多的朋友,我讓他們所有的時間提前,因爲他們在,我想已經環繞在字典其他各種性質和取得的節點,我怎麼接愛麗絲與那些朋友沒有創建額外的愛麗絲?我認爲合併會起作用,但我不明白它的語法。

回答

1

V3一直給我也適合,沒有例子。這對我來說很有用。 爲了合併工作,你需要設置唯一的約束。我不使用py2neo來設置我的數據庫約束。這是cypher命令在您的數據庫上運行一次。

Cypher支架代碼以在Neo4j的運行一次(如果也使用瀏覽器一次運行一個),用於應用

from py2neo import Graph,Node,Relationship,authenticate 
n1 = Node("Role",name="Manager") 
n2 = Node("Person",name="John Doe") 
n2['FavoriteColor'] = "Red" #example of adding property 
rel = Relationship(n2,"hasRoleOf",n1) #n2-RelationshipType->n1 
graph = Graph() 
tx = graph.begin() 
tx.merge(n1,"Role","name") #node,label,primary key 
tx.merge(n2,"Person","name") #node,label,pirmary key 
tx.merge(rel) 
tx.commit() 

CREATE CONSTRAINT ON (r:Role) 
ASSERT r.name IS UNIQUE 

CREATE CONSTRAINT ON (p:Person) 
ASSERT p.name IS UNIQUE 

Python代碼