0

I thought that MERGE is something like a combination of MATCH and CREATE, in pseudocode:

 MATCH (n...) IF n NOT EXISTANT -> CREATE (n...)

Why does a MERGE command fails if there is a unique constraint violation?

MasaZo
  • 43
  • 5

1 Answers1

1

This happens if you try to add a non-existent property or updating the existing property on a certain node. What happens then is that there is no node that is matched with that, so new node is created with the existing property that has a uniqueness constraint on it. For example if you set up uniqueness constraint on :User(name) and run

MERGE (n:User {name: "1"});

nothing will happen since that user exists. If you run

MERGE (n:User {name: "1", id: "1"});

again nothing will happen since that user exists. But, if you run

MERGE (n:User {name: "1", id: "1", age: 12});

there will be a uniqueness constraint violation, because there is no user with age property, hence there is no matched user with name 1, id 1 and age 12, and then the user should be created. It can't be created due to uniqueness constraint on property name.

The same will happen if you run:

MERGE (n:User {name: "1", id: "2"});

because there is no node that matches that query, it will be created and hence you'll get a uniqueness constraint violation.

Moraltox
  • 537
  • 1
  • 7