2016-08-04 131 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代码