Create relationship when NOT exist ELSE update (sounds like merge, but ..)

HI

I want to create a new relastionship between 2 nodes if the two nodes NOT already have one of the same type.
Else (if they have a relationshipt of that type) i want to reactivate it , but i have some syntax problems ... Can someone help me? THANKS ROB

:param orgguid => '549b28c2-378c-412d-8256-f5300a828200';
:param currentStatusguid => '16d25eed-caad-4415-936c-06b94cd0e050';
:param futurecurrentstatus => 'ed6a6d0f-0d5b-456d-baca-5a0803a854ad';
MATCH (node1 {guid: $orgguid}), (node2 {guid: $futurecurrentstatus})
WHERE NOT EXISTS ((node1)-[r:current_status]->(node2)) CREATE ((node1)-[:current_status]->(node2))
WHERE EXISTS ((node1)-[r:current_status]->(node2)) SET r.status = "active";

Hello @rob2 :slight_smile:

In your case, you should use MERGE:

MATCH (node1 {guid: $orgguid}), (node2 {guid: $futurecurrentstatus})
MERGE (node1)-[:current_status {status: "active"}]->(node2)

Regards,
Cobra

WOW - thanks Cobra . so fast! can you give me another hint? I tried to combine it with ON CREATE/MATCH like that:

:param orgguid => 'fd21ccc6-0933-415b-97e3-d88129301b8a';
:param currentStatusguid => 'b10eb05e-5808-4335-aa7f-f717d47626ba';
:param futurecurrentstatus => 'ed6a6d0f-0d5b-456d-baca-5a0803a854ad';
MATCH (node1 {guid: $orgguid}), (node2 {guid: $futurecurrentstatus})
MERGE ((node1)-[r:current_status {status: "active"}]->(node2))
ON CREATE SET (r.guid = randomUUID())
ON MATCH SET (r.timestamp_unix_changed = timestamp());

the error is

Invalid input 'ON': expected "." (line 4, column 1 (offset: 170))
"ON MATCH SET (r.timestamp_unix_changed = timestamp())"

You don't need to use parenthesis for MERGE and ON CREATE or ON MATCH:

:param orgguid => 'fd21ccc6-0933-415b-97e3-d88129301b8a';
:param currentStatusguid => 'b10eb05e-5808-4335-aa7f-f717d47626ba';
:param futurecurrentstatus => 'ed6a6d0f-0d5b-456d-baca-5a0803a854ad';
MATCH (node1 {guid: $orgguid}), (node2 {guid: $futurecurrentstatus})
MERGE (node1)-[r:current_status {status: "active"}]->(node2)
ON CREATE SET r.guid = randomUUID()
ON MATCH SET r.timestamp_unix_changed = timestamp();
1 Like

you saved my day! THANKS