nicolewhite / neo4j-flask

Flaskr Extended with Neo4j and Py2neo.
http://nicolewhite.github.io/neo4j-flask/
312 stars 114 forks source link

conflicting uniqueness constraints #17

Open jasonnet opened 7 years ago

jasonnet commented 7 years ago

Thanks for the great tutorial. Starting it up, two of us here both experienced problems with uniqueness constraint conflicts. We were eventually able to work around this by commenting out the uniqueness constraint check, but we don't know what the proper fix is.

This occurred even on a freshly deleted (and created) database. In my case, the flask server was running in debug mode and neo4j was running on special localhost ports.

Here is the stack trace:

Traceback (most recent call last):
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/database/http.py", line 203, in post
    response = self.__base.post(body, headers, **kwargs)
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/packages/httpstream/http.py", line 984, in post
    return rq.submit(**kwargs)
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/packages/httpstream/http.py", line 452, in submit
    return Response.wrap(http, uri, self, rs, **response_kwargs)
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/packages/httpstream/http.py", line 489, in wrap
    raise inst
py2neo.packages.httpstream.http.ClientError: 409 Conflict

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from blog import app
  File "/home/dad/jason_work/nicole/blog/__init__.py", line 4, in <module>
    graph.schema.create_uniqueness_constraint("User", "username")
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/database/__init__.py", line 775, in create_uniqueness_constraint
    {"property_keys": [property_key]})
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/database/http.py", line 212, in post
    raise_from(GraphError(message, **content), error)
  File "/home/dad/jason_work/nicole/venv_nicole/lib/python3.5/site-packages/py2neo/util.py", line 124, in raise_from
    raise exception
py2neo.database.status.ConstraintViolationException: Constraint already exists: CONSTRAINT ON ( user:User ) ASSERT user.username IS UNIQUE
ryancollingwood commented 6 years ago

Howdy

I've previously had similar issues when ensuring uniqueness with py2neo, the follow code snippet in __init__.py should see you right:

from .views import app
from .models import graph

def ensure_unique_constraint(graph, label, property_key):
    if (property_key not in graph.schema.get_indexes(label)):
        if property_key not in graph.schema.get_uniqueness_constraints(label):
            graph.schema.create_uniqueness_constraint(label, property_key)

ensure_unique_constraint(graph, "User", "username")
ensure_unique_constraint(graph, "Tag", "name")
ensure_unique_constraint(graph, "Post", "id")