Search code examples
memgraphdb

How can I ensure not to creat a duplicate relationship when creating one between existing nodes?


I've used the following code to create relationship between two nodes:

MATCH (c1), (c2)
WHERE c1.name = "UK" AND c2.name = "London"
CREATE (c2)-[:IN]->(c1);

If I run it few times it will duplicate the relationship. I want to avoid possible duplication. What can I do?


Solution

  • To avoid duplication of relationship can use the MERGE clause instead CREATE:

    MATCH (c1), (c2)
    WHERE c1.name = "UK" AND c2.name = "London"
    MERGE (c2)-[:IN]->(c1);