Reading various relationship types from a CSV file

I am trying to create a graph by loading two CSV files, one for nodes and one for relationships. Loading the nodes is easy. The relationships are of 6 different types:

CH_AGENTS, CLOSEST, COLLAB, COMMS, CULTURE, INNOV

and these are specified in column 1 of the CSV for the relationships.

Having successfully loaded the nodes, which represent people, I tried to use the Cypher below, but it doesn’t like the variable type for the relationship given as :row[0]. Any suggestions?

LOAD CSV FROM 'file:///ResponsesTable.csv'
    AS row
    MATCH (a:Person {fullName:row[1]})
    MATCH (b:Person {fullName:row[2]})
    MERGE (a)-[:row[0] {respondent:row[1], nominee:row[2], questonId: row[3], questionNo:row[4], questionTitle:row[4]}]->(b)

@gideonmitchell

1 Like

Try this. Change shown in bold.

LOAD CSV FROM 'file:///ResponsesTable.csv'
AS row
MATCH (a:Person {fullName:row[1]})
MATCH (b:Person {fullName:row[2]})
WITH a, b, row[0] as rel
MERGE (a)-[:$(rel) {respondent:row[1], nominee:row[2], questonId: row[3], questionNo:row[4], questionTitle:row[4]}]->(b)

1 Like

Yes, this is likely to work and utilizes dynamic labels, CREATE - Cypher Manual, as long as you have a new enough version (not entirely sure when it was added for CREATE/MERGE but in 2025.07 it was added in at least most places so as long as you're not very far behind you should be fine :smiley:)

You might even be able to do:

MERGE (a)-[:$(row[0]) {respondent:row[1], nominee:row[2], questonId: row[3], questionNo:row[4], questionTitle:row[4]}]->(b)

without the extra WITH clause.

2 Likes

Thank you so much Therese. This looks as if it should work after I have managed to upgrade my system (it rejected the $ sign on my original installation). On upgrading to Desktop 2.1.3 it now won’t let me connect to my cloned databases because it wants me to enter credentials that I hadn’t defined before. I think I’ll have to start again from scratch. Thanks anyway for your help.

Thanks ameyasoft (how do you pronounce that?). Looks promising once I have upgraded my system.

Thanks for your interest. ameyasoft is my pen name
Ameya is a Sanskrit word meaning "immeasurable". Derived from a- (not) and meya (measurable)

Check this:

@therese.magnusson

WITH requirement is in place for many years, Check this link published in 2017.

yeah, I think that might still be correct that if you have a MERGE and then a MATCH you need a WITH in between. However, you can have a MATCH followed by a MERGE without one, and the original query did that without issue (the issue was on trying to use row[0] as a relationship type directly without the dynamic labels syntax, we don't need to introduce it as a variable to then be used in the dynamic label but can just use it directly in the dynamic label expression without the middle step of making it a variable).