cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Create a relationship on the same node

smacharla
Node

I want to create a relationship with the same node

LOAD CSV WITH HEADERS FROM "file:///transactions.csv" AS line
WITH line
MERGE (c:Customer {loginId: line.`login_ID`, name: line.Customer_Name, age: line.Customer_Age, occupation: line.Customer_Occupation})

The above code is used to create a Customer node and then I write the code below to define a relationship called FRIEND on this same node.

LOAD CSV WITH HEADERS FROM "file:///transactions.csv" AS line
WITH line
MERGE (c:Customer {name:line.Customer_Name})
ON CREATE SET c.name = line.Customer
RETURN line
LOAD CSV WITH HEADERS FROM "file:///transactions.csv" AS line
WITH line
MATCH (c1:Customer {name:line.'Ben Thompson'}), (c2.Customer {name:line. 'Ricky Marty'})
MERGE (c1)-[:FRIEND]->(c2)

But when I run this code it doesn't work, what am I doing wrong here? The values 'Ben Thompson' and 'Ricky Marty' are present in the csv file and they appear as Ben, Thompson, and Ricky, Marty in the graph when I run
MATCH(n) RETURN(n)
since that's how they appear in the csv file being the first_name and last_name of a customer. Any help is appreciated. Thank you for your time in advance.

2 REPLIES 2

smacharla
Node
LOAD CSV WITH HEADERS FROM "file:///transactions.csv" AS line
WITH line
MATCH (c1:Customer {name:line.Customer_Name}), (c2:Customer {name:line.Customer_Name})
MERGE (c1)-[:FRIEND]->(c2)

This query works. I have a new question which would be that the above query creates a friend relationship with oneself. I don't want this and so I added a new line of code but I'm getting an error. This new code is as follows:

LOAD CSV WITH HEADERS FROM "file:///transactions.csv" AS line
WITH line
MATCH (c1:Customer {name:line.Customer_Name}), (c2:Customer {name:line.Customer_Name})
MERGE DISTINCT (c1)-[:FRIEND]->(c2)
WHERE c1.Customer_Name != c2.Customer_Name

Any help is appreciated. Thank you for your time in advance.

clem
Graph Steward

I think the WHERE clause has to come after the MATCH clause but before the MERGE (or a CREATE).