我想在py2neo v3中的两个现有的不同类型的现有节点之间创建关系,这只能使用Cypher执行来完成,还是应该执行此操作的函数(可能是合并)?
例如
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"))
关键是,如果爱丽丝有很多朋友,而我提前使他们全部成为朋友,是因为他们在字典中具有其他各种属性,而这些属性我已经遍历并创建了节点,那么如何在不创建其他对象的情况下将爱丽丝与这些朋友联系起来爱丽丝?我认为合并会起作用,但我不了解其语法。
V3也一直在为我提供帮助,目前尚无任何示例。这就是对我有用的东西。为了使合并生效,您需要设置唯一的约束。我不使用py2neo来设置数据库约束。这是在数据库上运行一次的cypher命令。
密码在Neo4j中运行一次(如果使用浏览器,一次也运行一次)
CREATE CONSTRAINT ON (r:Role)
ASSERT r.name IS UNIQUE
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS UNIQUE
适用的Python代码
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句