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
Without knowing your data, my hunch is that one of the
MATCHclauses 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 MATCHinstead. Then, we always produce at least one binding of the node variable and may it benull. In that case, the query would fail and tell you which node it could not find.