cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cannot delete node<id>, because it still has relationships. To delete this node, you must first delete its relationships

I am creating relations between nodes n and l.
Sometimes duplicate relations occur.
3X_b_3_b34a07b194eb9f245538bd926a9ed5a1930d98aa.png

If a relation of this type already exists, it should merge.
So to be clear: only the relation should merge - not the nodes.
Unfortunately I have not found something like apoc.refactor.mergeRels, som i am using mergeNodes.

I get this exception, that i need to delete the relationships first. But how do I integrate a deletion of relationships into this query?

Here are the query that gives the exception (same as above from picture)

MATCH(n) WHERE ID(n) = {fromNodeId}
MATCH(l) WHERE ID(l) = {toNodeId} 
CREATE (n)-[r:{relationType}]->(l)
WITH n,l
MATCH (n)-[r:{relationType}]->(l) 
WITH n ORDER BY n.creationTime DESC
WITH collect(n) AS origin 
CALL apoc.refactor.mergeNodes(origin, { properties: 'discard',  mergeRels:true}) 
YIELD node 
RETURN node

Thank you!

8 REPLIES 8

andy_hegedus
Graph Fellow

You can use the command

MATCH (n:yourNode)..
DETACH DELETE n

Andy

That's a good method

EDIT:

I have found apoc.merge.relationship
The problem of duplicate relations continues

WITH n,l
CALL apoc.merge.relationship
(
  n,'{relationType}',{},{},l,{}
) 
YIELD rel
RETURN rel 

Now even more so, as relation between nodes that are neither (n) nor (l) will be created and duplicated

Try this:

MATCH (a:Name)-[r]->(b:Name)
WITH a, collect(r) as rels, b
WHERE size(rels) > 1
UNWIND tail(rels) as rel
DELETE rel
return a, b

I'm getting this same error message while trying to use:

CALL apoc.refactor.mergeNodes([n, n2])

to merge duplicate nodes. Did you find a solution?

Same problema with apoc.refactor.mergeNodes([n1,n2])

To check the if the relationship already exists between two nodes and if it already exists then it should merge the relationship not the Nodes.

I have faced same issue of duplicate relationships getting created between two nodes in Knowledge Graph. 

Here is my simple approach for same..

MATCH (n) WHERE ID(n) = {$fromNodeId},
MATCH (l) WHERE ID(l) = {$toNodeId}
WHERE NOT EXISTS ((n)-[:$relationType]->(l))
MERGE (n)-[:$relationType]->(l)

Thank you.

Every time you run this query, you will create a new relationship with the 'create' statement. I suggest you use 'merge' instead.  You can them eliminate the 'match' too.

MATCH(n) WHERE ID(n) = {fromNodeId}
MATCH(l) WHERE ID(l) = {toNodeId} 
MERGE (n)-[r:{relationType}]->(l)
WITH n ORDER BY n.creationTime DESC
WITH collect(n) AS origin 
CALL apoc.refactor.mergeNodes(origin, { properties: 'discard',  mergeRels:true}) 
YIELD node 
RETURN node

I am not familiar with the syntax on line 3 that specifies the relationship type. Node labels and relationship types can not be parameterized.