Falcondance
Falcondance

Reputation: 90

Neo4j upsert with possibly new edge type

I am trying to create upsert logic in neo4j. I need to upsert two nodes and connect the two nodes with a relationship.

If there is an existing relationship that shares 3 specific properties, e1_id, e2_id, and model, then I need to completely overwrite that relationship with new data and possibly a new type.

For example, I have tried this:

MERGE (snapchat:ORG{name:'ORG_snapchat'})
MERGE (xbox:ORG{name:'ORG_xbox'})

MERGE (xbox)-[edge:{e1_id:'111', e2_id:'222', model:'TEST'}]->(snapchat)
ON CREATE
    SET edge:OPPO{e1_id:'111', e2_id:'222', url:'test.com', date:'2022-09-09', model:'TEST', context:'THIS IS A TEST'}
ON MATCH
    SET edge:OPPO{e1_id:'111', e2_id:'222', url:'test.com', date:'2022-09-09', model:'TEST', context:'THIS IS A TEST'};

However, this yields:

Invalid input '{': expected an identifier (line 4, column 20 (offset: 150))
"MERGE (xbox)-[edge:{e1_id:'222_xbox_test', e2_id:'111_snapchat_test', model:'TEST'}]->(snapchat)"

Because it is expecting me to specify an edge type before it can merge. I'm not sure what to do here.

Should I just write a query that deletes any matching edge and then creates a new edge?

Any help is appreciated, thanks.

Upvotes: 1

Views: 168

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6534

Edge has only exactly a single type. When you create it with MERGE you have to define it. Therefore -

Should I just write a query that deletes any matching edge and then creates a new edge?

Yes, if you can simply delete an existing edge than yes. Do that. Make sure to use an OPTIONAL MATCH, so that it works even if there is not existing edge.

Upvotes: 1

Related Questions