How to create a relationship only if it doesn't already exist?

I have a "messy" data source that has duplicates in it, and trying to process it cleanly into Neo4j.

Imagine the following graph:

MATCH (cmk:Make)-[r:hasModel]->(cmm:Model) RETURN cmk,r,cmm

I tried using MERGE but I guess it's for node properties only. How do I ensure that I don't have the r:hasModel relationship is present between two nodes exactly 0 or 1 times? In other words, I have created makes, and models, and when I process the relationships, sometimes I hit a duplicate and was looking for a single cypher that will create the r:hasModel relationship only if it doesn't already exist, between two target nodes.

I tried CREATE UNIQUE but it seems to kill my connection to Neo4j!


You can actually use MERGE on relationships as well. Things can be tricky sometimes though if you are trying to MERGE nodes and relationships simultaneously. Depending on the type of sources you are using, I generally like to break down node creation and relationship creating into separate steps to keep things simple and "modular".