gar
gar

Reputation: 168

Create Unique Makes Duplicate Nodes

What I am trying to do is make a relationship from a person to an AWARDED Degree node. There should only be one of these Degree nodes with MBA, and everyone that has an MBA will point to this one node.

The problem is, when I run this query with a startNode of a different person, it creates a new Degree{value:'MBA'} node. How can I have it all pointing to the same MBA node?

My Cypher Query:

START startNode=node(1)

CREATE UNIQUE
startNode-[:HAS_EDUCATION]->(nodeEducation1:Education{graduated_year:1901})

CREATE UNIQUE  nodeEducation1-[:AWARDED]->(a:Degree{value:'MBA'})

RETURN a;

Upvotes: 0

Views: 159

Answers (1)

Thomas Fenzl
Thomas Fenzl

Reputation: 4392

Create unique doesn't on its own try to match nodes [1]. What you can do is use the merge functionality to match the Degree Node (creating one only if necessary) and then create unique the relationship and intermediate nodes.

START startNode=node(9)
MERGE( phd:Degree {value: 'PHD'})
CREATE UNIQUE
startNode-[:HAS_EDUCATION]->(nodeEducation1:Education{graduated_year:1901})

CREATE UNIQUE  nodeEducation1-[:AWARDED]->phd

RETURN phd

Upvotes: 2

Related Questions