Search code examples
pythonneo4jpy2neo

setting neo4j uniqueness constraints in py2neo v3


Using v2 py2neo I could put this in __init__.py

graph.cypher.execute("CREATE CONSTRAINT ON (n:User) ASSERT n.username IS UNIQUE")

Why does v3 py2neo

graph.run("CREATE CONSTRAINT ON (n:User) ASSERT n.username IS UNIQUE")

fail with this error?

TypeError: unbound method run() must be called with Graph instance as first argument (got str instance instead)


Solution

  • You should declare the graph variable this way:

    >>> graph = Graph()
    

    instead of (without the brackets):

    >>> graph = Graph
    

    Also, alternatively to the graph.run() method you can use the graph.schema.create_uniqueness_constraint() method, like this:

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