I want to create a neo4j
database with users. I want the following properties to be unique:
- username
- token
What I've tried:
CREATE CONSTRAINT ON (user:User) ASSERT user.username IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.email IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.token IS UNIQUE
However, this does not prevent me from creating new nodes with repeated email, username or token. The image shows 5 nodes, all with the same data but only the first one is a User. I don't want this, I want neo4j
to return an error.
Is it possible?
Thanks
EDIT:
Neo4j
version: 2.2.3
And I use neoism
for Go
to insert data:
n, err := db.CreateNode(neoism.Props{"id": user.Id, "username" : user.Username,
"displayname" : user.Displayname,
"email" : user.Email, "token" : user.Token})
if err != nil {
return ERROR_NEO4J
}
n.AddLabel("User")
I finally solve it with a raw query with
neoism
. The original code created a node with no label and it was added later. At this moment, the constraint didn't allow the code to add the label but the node was already created.The solution is running a query that adds the label at the same time it creates the node: