Neo4j: Relationships

I am currently working on a project which aims to use graph databases, in particular Neo4j. My question concerns how to create the "Relationship" relations between the different nodes, for information, the data to be used is in CSV format, in this case, I will import data that is in .csv format, first we create the nodes based on this data, then we move on to creating the relationships between the different nodes.

I tried a lot of code but we didn't get the desired result regarding the creation of relationships between nodes.

I used the following code:

Load csv with headers from "file:///airports.csv" as airports create (a1: Airport {label: airports.label, city: airports.city, state: airports.state})
match (n) return (n)

Load csv with headers from "file:///flights.csv" as flights create (n: Flights {flight: flights.flight, airline: flights.airline, capacity: flights.capacity})
match(n) return(n)

while the nodes are created. but neither do relationships. in addition I had the following message: (no changes, no records) this after the execution of the following code:

Load csv with headers from "file:///flights.csv" as flights match (a: Flights {flight: flights.flight}), (b: Airport {label: flights.arrive}) create (a) –[r : Arrivals] -> (b)
match (n) return (n)

Load csv with headers from "file:///flights.csv" as flights match (a: Flight { flight: flights. flight}), (b: Airport {label: flights. depart}) create (a) –[r : Departures] -> (b)

match (n) return (n)

To inform you, I have two tables of data whose extension .csv one for airports and the other for flights. How should we create the relationship that exists between airports and flights? I don't know where is the error.

Hello @Survival :slightly_smiling_face:

Can you share CSV files?

Regards,
Cobra

neo4j relationship

I thank you for having answered me it is now or I saw your message following health reasons.

according to your post do you want me to send you the csv files i am working with

Pending a favorable response, please sir receive my best regards.

You can upload CSV files here.

Hello,

Thank you sir for answering me.
On https://community.neo4j.com/ (this Neo4j community) we cannot load files with extension .csv I thought of another solution and take screenshots and put them in .jpg image format in order to send them to you to display the content of my own .csv files.
Please accept sir my best regards.

Continuation of the previous message

Hello @Survival :slightly_smiling_face:

I took the liberty of optimizing the queries and changing the data model a bit. Also, I put all column names in CSV files in lowercase.

// Create unique constraints
CREATE CONSTRAINT constraint_Airport IF NOT EXISTS FOR (airport:Airport) REQUIRE airport.label IS UNIQUE;
CREATE CONSTRAINT constraint_Flight IF NOT EXISTS FOR (flight:Flight) REQUIRE flight.flight IS UNIQUE;
// Create Airport nodes
LOAD CSV WITH HEADERS FROM "file:///airports.csv" AS line 
MERGE (airport:Airport {label: line.label}) 
SET airport += {
	city: line.city, 
	state: line.state
};
// Create Flight nodes
LOAD CSV WITH HEADERS FROM "file:///flights.csv" AS line 
MERGE (flight:Flight {flight: line.flight}) 
SET flight += {
	airline: line.airline, 
	capacity: line.capacity
};
// Create relationships
LOAD CSV WITH HEADERS FROM "file:///flights.csv" AS line 
MATCH (flight:Flight {flight: line.flight}) 
MATCH (airport_start:Airport {label: line.depart}) 
MATCH (airport_end:Airport {label: line.arrive}) 
MERGE (flight)-[:ORIGIN]->(airport_start) 
MERGE (flight)-[:DESTINATION]->(airport_end);

Regards,
Cobra

thank you sir for answering me so soon
when I ran the following code:

// Create Airport nodes
LOAD CSV WITH HEADERS FROM "file:///airports.csv" AS line
MERGE (airport:Airport {label: line.label})
SET airport += {
city: line.city,
state: line.state
};
I am displaying the following message :

Cannot merge the following node because of null property value for 'label': (:Airport {label: null})

while my airports.csv file that I have already sent you a screen print contains four lines in total
can you help me to solve this error, really it is an urgent work that I must submit it.

waiting for a favorable response, please sir accept my best regards

Did you replace Label with label in your CSV files? Make sure the column names match the names in the query. In my answer, I didn't use your column names so you should check that.

I thank you Sir for having answered me, for the code that you sent me, I tried it in its entirety and I modified the labels according to my .csv table used while I have as results the non-display nodes and relationships.

For this purpose, I tried to modify the code in my way for the creation of the nodes and it worked well.

The code is the following:

// Creation of nodes related to airports

Load CSV With headers from “file:///airports.csv” as row Create (a:airports) set a= row {.label, .City, .State}

MATCH (a:airports) RETURN n LIMIT 4

// Creation of the nodes relating to the flights

Load CSV With headers from “file:///flights.csv” as row Create (b:flights) set b=row {.flight, .Depart, .Arrive, .Capacity}

MATCH (n:flights) RETURN n LIMIT 24

While I used your code to create the relationships, the code after modification of the labels according to my .csv tables is the following:

LOAD CSV WITH HEADERS FROM “file:///flights.csv” AS line

MATCH (flights: flights {flight: line. Flights})

MATCH (Depart: flights {Label: line.Depart})

MATCH (Arrive: flights {Label: line. Arrive})

MERGE (flights)-[: ORIGIN]-> (Depart]

MERGE (flights)-[: Destination]-> (Arrive);

when I got the following message: (no changes, no records)

according to the screen prints that I sent relating to my tables and the codes that I used and mentioned in this message, can you Sir show me where exactly the error is?

For information, at first, I used your code, which is the following:

// Create unique contraints

CREATE CONSTRAINT constraint_airports IF NOT EXISTS FOR (airports:Airports) REQUIRE airports.Label IS UNIQUE;

CREATE CONSTRAINT constraint_flights IF NOT EXISTS FOR (flights:flights) REQUIRE flights.flights IS UNIQUE;

Then I used the following code which didn't work, although I made appropriate corrections to its content according to my own .csv tables used.

// Create Airports nodes

LOAD CSV WITH HEADERS FROM file:///airports.csv as line

MERGE (airports: airports {Label: line. Label})

SET airport += {

          City: line. City,

State: line. State

};

//Create Flights nodes

LOAD CSV WITH HEADERS FROM file:///flights.csv AS line

MERGE (flights:flights {flight: line.flight})

SET flights += {

         Depart: line.Depart,

         Arrive: line.Arrive

         Capacity: line.Capacity

};

Waiting for a favorable response, please Sir receive my best regards.

Hello,

I sent you the last time a message containing in detail the code that I executed.

sir, I would like you to answer me as soon as possible, because I have to submit this task soon.

cordial greetings.

Hello @Survival,

I used these queries and they are working well. I don't have any errors. I'm using Neo4j 4.4.12.

// Create unique constraints
CREATE CONSTRAINT constraint_Airport IF NOT EXISTS FOR (airport:Airport) REQUIRE airport.label IS UNIQUE;
CREATE CONSTRAINT constraint_Flight IF NOT EXISTS FOR (flight:Flight) REQUIRE flight.flight IS UNIQUE;
// Create Airport nodes
LOAD CSV WITH HEADERS FROM "file:///airports.csv" AS line 
MERGE (airport:Airport {label: line.label}) 
SET airport += {
	city: line.city, 
	state: line.state
};
// Create Flight nodes
LOAD CSV WITH HEADERS FROM "file:///flights.csv" AS line 
MERGE (flight:Flight {flight: line.flight}) 
SET flight += {
	airline: line.airline, 
	capacity: line.capacity
};
// Create relationships
LOAD CSV WITH HEADERS FROM "file:///flights.csv" AS line 
MATCH (flight:Flight {flight: line.flight}) 
MATCH (airport_start:Airport {label: line.depart}) 
MATCH (airport_end:Airport {label: line.arrive}) 
MERGE (flight)-[:ORIGIN]->(airport_start) 
MERGE (flight)-[:DESTINATION]->(airport_end);

Here is the content of the airports.csv file:

label

city

state

DTW

Detroit

Michigan

ATL

Atlanta

Georgia

PIT

Pittsburguer

Pennsylvana

BOS

Boston

Massachussette

Here is the contents of the flight.csv file:

flight

airline

depart

arrive

capacity

23

DELTA

ATL

DTW

160

24

DELTA

DTW

ATL

160

27

DELTA

DTW

BOS

160

28

DELTA

BOS

DTW

160

35

DELTA

DTW

PIT

128

36

DELTA

PIT

DTW

128

37

DELTA

ATL

BOS

160

38

DELTA

BOS

ATL

160

43

DELTA

ATL

PIT

160

44

DELTA

PIT

ATL

160

45

DELTA

BOS

PIT

160

46

DELTA

PIT

BOS

160

101

Southwes

DTW

BOS

136

102

Southwes

BOS

DTW

136

103

Southwes

DTW

PIT

136

104

Southwes

PIT

DTW

136

1231

American

DTW

BOS

160

1232

American

DTW

PIT

160

1257

American

DTW

ATL

128

I don't know why it's not working on your computer.

Here is the resulting graph:

graph.png

Best regards,
Cobra

Can you share your CSV files please? You can replace the extension of the file by txt to upload them here normally.

when attaching the .txt file I got an error message as demonstrated in the previous message

(migrated from khoros post https://community.neo4j.com/t5/neo4j-graph-platform/neo4j-relationships/m-p/61772#M36531)

thank you sir for answering me
please find attached the files whose extension is .txt.

can you sir show me how to transfer a file whose extension is .xlsx to a file in the extension is .csv
maybe the errors are caused when I transfer between files, ie from .xlsx to .csv.

cordial greetings

It's not possible anymore but look at my last message, I created the same dataset as you and it works.

I don't know what else to say.

Hello,
I inform you sir that the code you sent me worked well the problem that I encountered last time is mainly due to the .csv format that I used, well I set the format and I rewrote the code you sent me and it worked fine.
I sincerely thank you.
Good for the moment I have another question to ask you if it is possible, based on the previous code, I wrote the following code:
// Create stop_times nodes
LOAD CSV WITH HEADERS FROM "file:///stop_times.csv" AS line
MERGE (stop_times:stop_times {stop_id:line.stop_id AND trip_id:line.trip_id})
SET stop_times+= {
arrival_time: line. arrival_time,
departure_time: line.departure_time,
stop_sequence: line.stop_sequence,
stop_headsign: line.stop_headsign
};
While I got the following message:

Neo.ClientError.Statement.SyntaxError: Variable `trip_id` not defined (line 2, column 56 (offset: 117))
"MERGE (stop_times:stop_times {stop_id:line.stop_id AND trip_id:line.trip_id})"

How should we settle it? I send you the tables used in the form of images for better understanding.
the question is when I will have a table whose primary key is the concatenation of two other keys which are primary keys each in its own table. in this case how can I express this using the cyfer language.
Pending a favorable response, please sir receive my best regards.

Your error is due to invalid syntax. Replace the ‘AND’ with a comma.

I will let @Cobra continuing helping you with your solution.

Hello,
I sincerely thank you sir for having answered me, I tried what you told me and I replaced the AND by the comma so I received the following message:
Neo.ClientError.Schema.ConstraintValidationFailed: Node(294) already exists with label `stop_times` and property `trip_id` = '1'
so, how should it be resolved?
waiting for a favorable response please accept sir my best regards