Using MERGE with relationships doesn't copy link properties

Hi guys!

I have one relationship type (exported products from one country to another) named "EXPORTED_PRODUCT". On top of that i've created second relationship type based on the product code, as stated here:

MATCH (c1:Country)-[r:EXPORTED_PRODUCT]->(c2:Country) WHERE r.ProductHSCode="300220" MERGE (c1)-[:EXPORTED_PRODUCT_300220]->(c2)

Eveything looks fine, expect that any other link property, such as "ProductValue" and "ProductQuantity" don't copy at all.

Any ideas how can i duplicate these properties to the new relationship type?

SET r2 = r1
Need to be tested not sure, you need to create the r2 variable for your new relation as you did with the first one

Hi @tard.gabriel can you please make an example with the Cypher query? I'm not quite sure how to replicate what you've described...

MATCH (c1:Country)-[r1:EXPORTED_PRODUCT]->(c2:Country)
WHERE r.ProductHSCode="300220"
MERGE (c1)-[r2:EXPORTED_PRODUCT_300220]->(c2)
ON CREATE
SET r2 = r1

Not tested

1 Like

@tard.gabriel it's not working. I've even tried with "WHERE r1.ProductHSCode="300220"" but no success whatsoever. Any other ideas perhaps?

Try this, it should work

@tard.gabriel

for some reason your query returns "(no changes, no records)", but this works:

MATCH (c1:Country)-[r:EXPORTED]->(c2:Country)
WHERE r.ProductHSCode="300220"
MERGE (c1)-[t:EXPORTED_PRODUCT_02]->(c2)
SET t.ProductHSCode=r.ProductHSCode,
t.ProductValue=r.ProductValue,
t.ProductQuantity=r.ProductQuantity,
t.Year=r.Year


It's simply because your relation already exist, that's why it's ON CREATE SET.
It will copy r1 in r2 only if the relation is created, if you want to do it systematically, your query is right. If you want to do it only when it matches an existing relationship you can use ON MATCH SET, so to recap you have :

SET
ON CREATE SET
ON MATCH SET

You also use both ON CREATE and ON MATCH when you need to define a different behaviour for each of them.

I would like to note that creating a new relationship type for each product is not a good practice. The property ProductHSCode on the relation EXPORTED_PRODUCT is quite sufficient to distinguish that. You just duplicate information with creating those new relations.

To clarify this exactly, we would need to know the full use case for these relationships and how they are queried later.

Best,
Reiner

Indeed, @Reiner got a point you won't be able to generalise a query and optimise it with parameters.