Neo4j: Relationships

You have a uniqueness constraint on property ‘trip_id’ for label ‘stop_times’. In your code, you ‘merge’ on two properties: stop_id and trip_id. Your data must not have an existing node with that combination of values, so it is created. You get the violation, because you already have a node with the value of trip_id.

MERGE (stop_times:stop_times {stop_id:line.stop_id AND trip_id:line.trip_id})

the solution is to match using only the combination of property that uniquely identifies that entity. All other properties are added using ‘set’.

assuming that ‘trip_id’ is your primary key for ‘stop_times’ label, you could modify your query to the following:

LOAD CSV WITH HEADERS FROM "file:///stop_times.csv" AS line
MERGE(stop_times:stop_times{trip_id:line.trip_id})
SET stop_times+= {

stop_id: line.stop_id,

arrival_time: line. arrival_time,
departure_time: line.departure_time,
stop_sequence: line.stop_sequence,
stop_headsign: line.stop_headsign
};

I thank you sir for having answered me I tried it and it worked well.

but I inform you sir that the primary key of the stop_times table has as primary key the combination of the two keys stop_id and trip_id so in the code which allows to create the unique constraints for each class I used the code below:

CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.trip_id IS UNIQUE;
CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.stop_id IS UNIQUE;

while according to the attached image it shows that if I take the trip_id column alone there are repeated cases while in the definition of the primary key the latter must not be repeated and if I take the column relating to stop_id only I will have the same case of the values which are repeated whereas if I combine these two columns I will have a unique primary key which checks the definition of the primary key the question is how to define the unique constraint on two columns whose combination forms our primary key of the "stop_times" table
Ok for that I used the following code:

CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.trip_id IS UNIQUE;
CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.stop_id IS UNIQUE;

in addition, the code you sent me is certainly successful, but logically I should not consider trip_id alone as the primary key of the "stop_times" table because there are repeating values.

In this case I would like to have a code that verifies this.

pending a favorable response, please accept sir my best regards.

Since your primary key for that label is a composite key consisting of trip_id and stop_id, you should drop the all constraints these two properties and create one node key constraint on the two properties jointly.

CREATE CONSTRAINT stop_times_node_key IF NOT EXISTS FOR (n:stop_times) REQUIRE (n.stop_id, n.trip_id) IS NODE KEY

A node key constraint requires uniqueness and existence. It also creates a composite index.

After doing this, your original code with the ‘merge’ using the two properties should work as you need it.

97DFFB47-54D4-42A3-82FF-44C57A313465.jpeg

thank you sir for answering me
I added the line of code you sent me regarding the constraint instead of the two lines of antecedent.

I attach below the complete code that I used as well as the results obtained.

to inform you about the stop_times table which includes the primary key composed when creating the relationship that exists between this table and the two tables trips and stops (in which its main key is composed of the two primary keys of these two tables: trips and stops, I got the following error message:

"Neo.DatabaseError.Statement.ExecutionFailed: java.net.URISyntaxException: Illegal character in path at index 6: file:/stop_times.csv"

How do you think this should be resolved?
waiting for a favorable response, please accept sir my best regards.

// Create unique constraints agency routes trips stop_times calendar stops

CREATE CONSTRAINT constraint_agency IF NOT EXISTS FOR (agency:Agency) REQUIRE agency.agency_id IS UNIQUE;

CREATE CONSTRAINT constraint_route IF NOT EXISTS FOR (route:routes) REQUIRE route.route_id IS UNIQUE;

CREATE CONSTRAINT constraint_trips IF NOT EXISTS FOR (trip:trips) REQUIRE trip.trip_id IS UNIQUE;

//CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.trip_id IS UNIQUE;

//CREATE CONSTRAINT constraint_stop_times IF NOT EXISTS FOR (stop_time:stop_times) REQUIRE stop_time.stop_id IS UNIQUE;

CREATE CONSTRAINT stop_times_node_key IF NOT EXISTS FOR (n:stop_times) REQUIRE (n.stop_id, n.trip_id) IS NODE KEY

CREATE CONSTRAINT constraint_calendar IF NOT EXISTS FOR (calendar:calendar) REQUIRE calendar. service_id IS UNIQUE;

CREATE CONSTRAINT constraint_stops IF NOT EXISTS FOR (stop:stop) REQUIRE stop.stop_id IS UNIQUE;

//. Creation des noeuds pour chaque classes d’entités agency routes trips stop_times calendar stops.

// Create agency nodes

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

MERGE (agency:agency {agency_id: line.agency_id})

SET agency+= {

            agency\_name: line. agency\_name,

            agency\_url: line. agency\_url,

          agency\_timezone: line. agency\_timezone,

         agency\_phone: line. agency\_phone,

         agency\_lang: line. agency\_lang,

         agency\_fare\_url: line. agency\_fare\_url

};

// Create routes nodes

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

MERGE (route:routes {route_id: line. route_id })

SET route += {

            agency\_id: line.agency\_id,

            route\_short\_name: line. route\_short\_name,

          route\_long\_name: line.route\_short\_name,

          route\_type: line.route\_type

};

// Create trips nodes

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

MERGE (trip:trips {trip_id: line.trip_id})

SET trip+= {

            route\_id: line. route\_id,

            service\_id: line. service\_id,

          trip\_headsign: line. trip\_headsign

};

// Create calendar nodes

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

MERGE (calendar:calendar {service_id: line.service_id})

SET calendar+= {

            monday: line.monday,

            tuesday: line.tuesday,

        wednesday: line.Wednesday,

        thursday: line.Thursday,

        friday : line.Friday,

         saturday: line. Saturday,

        sunday: line. Sunday,

        start\_date: line. start\_date,

        end\_date: line. end\_date

};

// 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, 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

};

// Create stops nodes

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

MERGE (stops:stops {stop_id: line.stop_id})

SET stops+= {

        stop\_desc: line. stop\_desc,

         stop\_lat: line. stop\_lat,

       stop\_lon: line. stop\_lon,

        zone\_id: line. zone\_id,

        loc\_type:line. loc\_type,

        stop\_name :line. stop\_name

};

// create relationships between (agency p, routes f : agency_id) (routes p, trips f : route_id) (calendar p, trips f : service_id) (stops p, stop_times f : stop_id) (trips p, stop_times f : trip_id)

// create relationships between (agency p, routes f : agency_id)

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

MATCH (route:routes {route_id: line.route_id})

MATCH (agency:agency {agency_id: line.agency_id})

MERGE (route)-[:related]->(agency);

// create relationships between (routes p, trips f : route_id)

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

MATCH (trip:trips {trip_id: line.trip_id})

MATCH (route:routes {route_id: line.route_id})

MERGE (trip)-[:related1]->(route);

// create relationships between (calendar p, trips f : service_id)

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

MATCH (trip:trips {trip_id: line.trip_id})

MATCH (calendar:calendar {service_id: line.service_id})

MERGE (trip)-[:related2]->(calendar);

// create relationships between (stops p, stop_times f : stop_id)

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

MATCH (n: stop_times {trip_id: line.trip_id, stop_id: line. stop_id})

MATCH (stop:stops {stop_id: line.stop_id})

MERGE (n)-[:related3]->(stop);

// create relationships between (trips p, stop_times f : trip_id)

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

MATCH (n: stop_times {trip_id: line.trip_id, stop_id: line. stop_id})

MATCH (trip:trips {trip_id: line.trip_id})

MERGE (n)-[:related4]->(trips);

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

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

(migrated from khoros post Re: Neo4j: Relationships - Neo4j - 61325)

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

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

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

(migrated from khoros post Re: Neo4j: Relationships - Neo4j - 61325)

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

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