在 py2neo v3 中设置 neo4j 唯一性约束

setting neo4j uniqueness constraints in py2neo v3

使用 v2 py2neo 我可以把它放在 __init__.py

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

此外,除了 graph.run() 方法,您还可以使用 graph.schema.create_uniqueness_constraint() 方法,如下所示:

>>> graph.schema.create_uniqueness_constraint("User", "username")