Relationship refuses to generate in Neo4j

25 Views Asked by At

Ive been trying to generate a relationship between two already existing nodes using a csv file, the code looks like this:

LOAD CSV WITH HEADERS FROM "major.csv" AS row
MATCH(std:students {snum:toInteger(row.snum)})
MATCH (deg:degrees {name:row.name, level:row.level})
CREATE (std)-[:major]->(deg);

where students and degrees are already created. But for some reason there aren't relationships being created. Any help would be appreciated!!

Creating students:

LOAD CSV WITH HEADERS FROM "students.csv" AS row
CREATE(std:students {
    snum:toInteger(row.snum),
    ssn:toInteger(row.ssn),
    name:row.name,
    gender:row.gender,
    dob:row.dob,
    c_addr:row.c_addr,
    c_phone:row.c_phone,
    p_addr:row.p_addr,
    p_phone:row.p_phone
});

Creating degrees:

LOAD CSV WITH HEADERS FROM "degrees.csv" AS row
CREATE (dgr:degrees {
    name:row.name,
    level:row.level
});

I have spent ages looking up stuff online and couldn't find anything that fixed it. I got a relationship out of something I tried, but it wasn't the correct one, so I couldn't query anything from one node to another.

LOAD CSV WITH HEADERS FROM "major.csv" AS row
CREATE(maj:major)
WITH maj, row
MATCH (std: students {snum:toInteger(row.snum)})
CREATE (std)-[:major]->(maj)
WITH maj, row
MATCH (deg: degrees {name:row.name, level:row.level})
CREATE (deg)-[:administers]->(maj);

This just added a relationship from the student to a random node called major with only an id

2

There are 2 best solutions below

0
Arne On

Without knowing your data, my hunch is that one of the MATCH clauses does not find the node. In that case, we would silently not create any relationship.

You could try to test this theory by using OPTIONAL MATCH instead. Then, we always produce at least one binding of the node variable and may it be null. In that case, the query would fail and tell you which node it could not find.

0
cybersam On

Since you are able to find a student using row.snum, it looks like you are not able to find degrees using row.name and row.level from "major.csv".

Check for differences between "degrees.csv" and "major.csv" in their name and level values. Perhaps the values are formatted differently, or perhaps the 2 files just have no overlapping degree data.