Let's say that we have n nodes with label :Test and a unique property called type.
UNWIND[{ type:"a" }, { type:"b" }, { type:"c" }, { type:"d" }] AS x
MERGE (t:Test { type: x.type })
RETURN t
That looks like this
Now let's introduce a node of label :Collection. The purpose if this node is to have a unique relationship pattern with the :Test nodes.
MATCH (a:Test { type:"a" }),(b:Test { type:"b" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a),(x:Collection)-[:HAS]->(b)
Return *
The problem that I face starts occurring when I try to make another unique structure, like the previous one, but with some nodes in common.
MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a)
RETURN *
The expected result is that another node of label :Collection gets created and linked to :Test {type:"a"} but the actual result is that it matches the previous data structure and returns that instead of creating a new one.
The expected result should have 2 :Collection nodes, one linked to type:"a", the other one linked to type:"a" and type:"b".
Any input kind of input will be very appreciated :D
From the neo4j docs on CREATE UNIQUE
:
CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE UNIQUE will always make the least change possible to the graph — if it can use parts of the existing graph, it will.
You add Collection
nodes without any properties. I think if CREATE UNIQUE
finds a Collection
node, it will use it. This is how CREATE UNIQUE
is supposed to work.
So if you want a new Collection
that is linked to some Test
nodes, you can either add some unique properties to the node:
MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection {key: 'unique value'})-[:HAS]->(a)
RETURN *
Or create it in a separate step:
MATCH (a:Test { type:"a" })
CREATE (x:Collection)
CREATE (x)-[:HAS]->(a)
RETURN *
Or use MERGE
instead of CREATE UNIQUE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With