Merge nodes by a variable string, not a constant string

This works fine:

merge (s:subject{name:sub})
merge (o:object{name:obj})
merge (s)-[:pre]->(o)

however, I want change relationship between s and o by another like this:

load csv with headers
from 'file:///knowledge_triple.csv'
as line
with line.Subject as sub, line.Predicate as pre, line.Object as obj
merge (s:subject{name:sub})
merge (o:object{name:obj})
merge (s)-[pre]->(o)

this cannot be correct, I wonder if it's possiable. 3q

Try this:

CALL apoc.create.relationship(s, pre,{}, o) YIELD rel
REMOVE rel.noOp

// REMOVE rel.noOp is a dummy script as we have to have a return value.

I'm not clear what your question is, but I do believe you have a typo. There is a missing colon before the label pre

merge (s)-[pre]->(o)

should

merge (s)-[:pre]->(o)

I do recommend the style guide of using all uppercase for Labels as it makes it easier to detect see these errors:

merge (s)-[:PRE]->(o)

In this particular case, I think what they were asking for was a way to create the type of the relationship dynamically (from a variable, parameter, or other derived value), so the lack of the colon for the type was intended here (though good eyes, these typos are common).

And dynamic relationship type isn't supported in Cypher (nor is dynamic node labeling), so APOC procs would be the workaround, as ameyasoft provided.

1 Like