Reputation: 61
I currently have the following model: (Team Leader)-[:leads]->(Team)
As we all know, changes happen within an organization. Today we might have:
(Richard)-[:leads]->(Team_1)
(Henry)-[:leads]->(Team_2)
and tomorrow:
(Henry)-[:leads]->(Team_1)
(John)-[:leads]->(Team_2)
(Henry)-[:leads]->(Team_3)
My current code is capable of loading a CSV file and do all these changes EXCEPT to delete the relationship between Richard and Team_1.
Current implementation I have is the following one:
LOAD CSV WITH HEADERS FROM "file:///TLsandTeams_1st.csv" AS csvLine
MERGE(tl:TL {name: csvLine.TL})
MERGE(t:Team {name: csvLine.Team})
CREATE UNIQUE (tl)-[:leads]->(t)
This creates the first scenario described above. Then the second step is:
LOAD CSV WITH HEADERS FROM "file:///TLsandTeams_2nd.csv" AS csvLine
MERGE (tl:TL {name : csvLine.TL})
WITH csvLine, tl
MERGE (t:Team {name : csvLine.Team})
WITH tl,t
OPTIONAL MATCH(tl)-[l:leads]->()
WITH tl,t,l
DELETE l
WITH tl,t
MERGE (tl)-[:leads]->(t);
This even works for the cases where a team didnt have a team leader associated and now has it and vice-versa. As I said before, all changes are correctly put into place except deleting the link between Richard and Team_1. Actually I would like to go even further and delete the Richard node if possible.
I was searching for a way of doing this without having to delete all Team Leader nodes and recreating them again.
Any help would be really appreciated. Thanks!
Upvotes: 0
Views: 52
Reputation: 359
Not sure but, if your OPTIONAL MATCH works on the team instead of the leader:
LOAD CSV WITH HEADERS FROM "file:///TLsandTeams_2nd.csv" AS csvLine
MERGE (tl:TL {name : csvLine.TL})
WITH csvLine, tl
MERGE (t:Team {name : csvLine.Team})
WITH tl,t
OPTIONAL MATCH ()-[l:leads]->(t)
WITH tl,t,l
DELETE l
WITH tl,t
MERGE (tl)-[:leads]->(t);
Upvotes: 1