I’m trying to transfer data from sql database to neo4j.
I have created company nodes, number nodes and relationship between them in neo4j.
(n:company{un_id:’11111’})-[r:using]->(m:phonenumber{N:‘555112233’})
Sometimes, companies are changing its number and I want to set new relationship like
Match
(n:company{un_id:’11111’}),
(m:phonenumber{N:‘555445566’})
Merge
(n)-[r:using]->(m)
Return id(r)
So if there is no more relationship between company and old phone number, how can I set previous relationship as old rel?
>Solution :
Using a status property on the relationship could help you, for instance like this
WITH '555445566' AS newNumber,
'11111' AS companyID
//set existing r.status to 'old'
MATCH (n:company {un_id:companyID})-[r:using]->(ph:phonenumber)
WHERE ph.N <> newNumber AND r.status <> 'old'
SET r.status = 'old'
// make sure to remove duplicates in case previous statement returns multiples
WITH DISTINCT newNumber, n
// create the new rel
MERGE (newph:phonenumber {N:newNumber})
MERGE (n)-[r:using {status:'current'}]->(newph)
RETURN id(r)