设置Neo4j的唯一性约束在py2neo V3
问题描述:
使用V2 py2neo我可以把这个__init__.py
设置Neo4j的唯一性约束在py2neo V3
graph.cypher.execute("CREATE CONSTRAINT ON (n:User) ASSERT n.username IS UNIQUE")
为什么V3 py2neo
graph.run("CREATE CONSTRAINT ON (n:User) ASSERT n.username IS UNIQUE")
失败,此错误?
TypeError: unbound method run() must be called with Graph instance as first argument (got str instance instead)
答
你应该声明变量是这样的:
>>> graph = Graph()
,而不是(不带支架):
>>> graph = Graph
此外,作为备选的graph.run()
方法,你可以使用graph.schema.create_uniqueness_constraint()方法,如下所示:
>>> graph.schema.create_uniqueness_constraint("User", "username")
你如何声明'graph'变量? –
哇!你从错误信息中发现了我的错字...我忘了图中的括号= Graph()你怎么做到的? – user1613312
我想你回答了我的问题,但我不能评论你的评分? – user1613312