Apoc.merge.relationship() creates duplicates in Neo4j

302 Views Asked by At

I am trying to create relationship between two nodes using apoc.merge.relationship, but it creates two same relationships, which I can see by search. They both have same direction and everything is the same although from query it's obvious that newLink.id is identifier. I hope someone can show me what is wrong with my cypher query.

UNWIND [{ 
     color:'#82abd1', direction:'true', id:'q', index:0, linkType:'a', 
     source:'46166.a690c888-e3d5-41ed-8469-79a88cce8388', status:'approved',  
     target:'46163.a690c888-e3d5-41ed-8469-79a88cce8388', type:'Applies for', value:2 
}] AS newLink
    MATCH 
      (fNode:Node {id: newLink.source}),
      (sNode:Node {id: newLink.target})
    CALL apoc.merge.relationship(
      fNode, 
      'Label', 
      {id: newLink.id}, 
      apoc.map.clean(newLink, ['id','type'],[]), 
      sNode, 
      apoc.map.clean(newLink, ['id','type'],[])
    )
    YIELD rel
    RETURN DISTINCT 'true';

my search query is

MATCH ()-[rel]-() RETURN COUNT(rel)
1

There are 1 best solutions below

0
Armen Sanoyan On

My query was finding same relationships for both (node1)-[rel]-(node2) and for (node2)-[rel]-(node1). So one way to avoid this situation is using ID(node1)>ID(node2), which compares nodes id given by neo4j.