Uniqueness constraint does not prevent from adding same data on Neo4j

250 Views Asked by At

I want to create a neo4j database with users. I want the following properties to be unique:

  • username
  • email
  • 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

graph

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")
2

There are 2 best solutions below

0
On BEST ANSWER

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:

cq := neoism.CypherQuery {
    Statement: `CREATE (n:User {id:{id}, username:{username},
        displayname:{displayname}, email:{email},
        token:{token}}) RETURN n`,
    Parameters: neoism.Props {
        "id": user.Id,
        "username" : user.Username,
        "displayname" : user.Displayname,
        "email" : user.Email,
        "token" : user.Token,
    },
}
err := db.Cypher(&cq)
0
On

A uniqueness constraint is associated with a label and property pair. All of your uniqueness constraints involve the User label, so neo4j will just enforce uniqueness on User nodes.

If you think it is appropriate, you can modify your uniqueness constraints so that they involve some other label (say, Base), and assign that label to all your nodes. neo4j allows a node to have multiple labels, so you can keep using the User label as well (but not as part of a constraint).