Trying to create Nodes and Relationship - failing to update

Hi there, I'm new to Neo4j and Cypher

I got Neo4j running in docker Version: 5.7.0, Edition:Community.

I can't update my Nodes with Relationships.

I frist create my nodes.

LOAD CSV WITH HEADERS FROM 'file:///nodes.csv' AS row
CREATE (:Place {user_id: row.user_id
	, full_name: row.full_name
	, category_name:row.category_name
	, latitude: toFloat(row.lat)
	, longitude:toFloat(row.long)
})

Then

CREATE CONSTRAINT Place_user_id
FOR (Place:user_id) REQUIRE Place.user_id IS UNIQUE

All good until now
Now I want to update the Nodes with Relationships

LOAD CSV WITH HEADERS FROM 'file:///relationship.csv' AS row
MATCH (from:Place {properties.user_id: row.from_id})
MATCH (to:Place {properties.user_id: row.to_id})
MERGE (from)-[: CONNECTED]->(to)

But all I get is (no changes, no records). Have I set up node correct? I have done alot of google, ChatGPT and GraphAcademy

Hello @PrincipalJohnShepher and welcome to the Neo4j community :slight_smile:

First of all, beware the constraint must be created first.
Moreover, can you share the CSV files please? (If you want to uplaod them, you will have to change the extension to .txt).

Best regards,
Cobra

Thanks cobra.

I don't think new use can upload files.
Sorry, new users can not upload attachments.

nodes.csv

"user_id","full_name","category_name","lat","long"
"54079522293",Gaijin,Restaurant,"55.689321","12.562655"
"57535946768",RESTO BAR,Restaurant,"55.671813","12.555052"
"8496078591",Salon,Restaurant,"55.686276","12.591689"
"214696586","Grønnegade 32 , 1107 Kbh K",Restaurant,,
"50227089672",Lago,Restaurant,,
"23080631894",Polly,Restaurant,"55.675437","12.546343"
"52665034563",L A M A R,Restaurant,"55.673497","12.555444"
"56239904769",Goldie,Restaurant,"55.688419","12.554486"
"49969640299",DONDA Deli,Restaurant,"55.666888","12.549846"
"48438034390",Restaurant MARK - Bistro & Bar,Restaurant,,
"49872006545",En•og•tredive•hundrede,Restaurant,,
"54151825764",OBERRA,Restaurant,,
"46725137067",Restaurant Camino,Restaurant,"55.668232","12.557575"
"46914356925",CIRCOLO,Tuscan Restaurant,,
"48548306416",Restaurant Goldfinch,Restaurant,,
"53976360585",Bredo,Bar,,

relationship.csv

"to_id","from_id"
"21448923621","53976360585"
"50312940387","53976360585"
"34320962263","53976360585"
"9936242184","214696586"
"45643908578","53976360585"
"49743934741","53976360585"
"46914356925","53976360585"
"47876370776","214696586"
"49969640299","53976360585"
"7998862321","53976360585"
"48548306416","53976360585"
"56920038394","53976360585"
"8629271169","214696586"
"45801797375","53976360585"
"178261858","53976360585"
"1771947998","53976360585"
"54931603949","53976360585"
"4840560141","53976360585"
"54151825764","214696586"
"50134185385","53976360585"
"43999194104","53976360585"
"33715629884","53976360585"
"46137802308","214696586"
"48788089515","53976360585"
"46000083054","53976360585"
"1425832870","53976360585"

Your error is in the MATCH clause.

First, you create the UNIQUE CONSTRAINT:

CREATE CONSTRAINT Place_user_id FOR (Place:user_id) REQUIRE Place.user_id IS UNIQUE;

Then, you create the nodes:

LOAD CSV WITH HEADERS FROM 'file:///nodes.csv' AS row
MERGE (place:Place {user_id: row.user_id})
SET place += {
    full_name: row.full_name, 
    category_name: row.category_name, 
    latitude: toFloat(row.lat), 
    longitude: toFloat(row.long)
};

Finally, you can create relationships:

LOAD CSV WITH HEADERS FROM 'file:///relationships.csv' AS row
MATCH (from:Place {user_id: row.from_id}) 
MATCH (to:Place {user_id: row.to_id}) 
MERGE (from)-[:CONNECTED]->(to)

Please note the filenames may be different and that some of your TO IDs are not in the nodes file, so some relationships are not created since the node does not exist.

Best regards,
Cobra

1 Like

Thank you. It works!
The next thing for me is to create the missing nodes based on the relationships and then use some model to explore them.

1 Like