I am having some sort of a tricky situation, and I would like some guidance on it. I have a CSV file with same-name columns because of their unique relationships. However, when I build the graph I have relationships that return to the same node which I think should not have happened since I merged their node labels. What am I doing wrong? why are some nodes returning the relationship as illustrated below:
@glilienfield thanks for the education on the map keys. So I tried the approach you illustrated. However, instead of creating a relationship from A-[rel]->B, it rather creates nodes from the relationship column row[1] in the table and connects them to each other, while row[0] and row[2] are not connected.
That is expected behavior, as the sgd and sgd1 aliases reference the same node. As such, the relationship created will relate back to the same node. The cause of your confusion is that you think you are utilizing the two "Solar Grade" columns. What is happen is that your query is only using the value of "Solar Grade" from the second column. This occurs because when you use "load csv with headers" a map is created for each row with the column names are the map keys. Since you have a duplicate column name, the first value gets replaced when the second value is added to the row's map.
If you don't want to change the column names, you can avoid this by not using 'with headers'. This will then create an array of values for each row instead of a map. You then access the columns using an index, such as row[0], row[1], and row[2]. You will also need to skip the first row in the file since your file has header data in the first row.
The issue I had was with the indexing, which was an oversight on my end. So instead of row[0] in the solution you provided, I should have used row[1]-[: row[2]]-> row[3] in my case. Yep! so you're right.
Besides I was trying to maintain the "relationship property" as indicated in the .csv file, since they vary for each row (i.e. avoid a common relationship name). Unfortunatley, my queries gave me syntax errors. However, I was able to resolve it with this approach.
LOAD CSV WITH HEADERS FROM "file:///similar_grades.csv" AS row
MERGE (sgd:Solar_grade {name: row.Solar_grade})
MERGE (sgd1:Solar_grade {name: row.Solar_grade_1})
WITH sgd, sgd1, row
WHERE row.Relationship = "isSimilarTo"
CREATE (sgd)-[:SIMILAR_TO]->(sgd1)
RETURN sgd, sgd1
LOAD CSV WITH HEADERS FROM "file:///similar_grades.csv" AS row
MERGE (sgd:Solar_grade {name: row.Solar_grade})
MERGE (sgd1:Solar_grade {name: row.Solar_grade_1})
WITH sgd, sgd1, row
WHERE row.Relationship = "isSameAs"
CREATE (sgd)-[:SAME_AS]->(sgd1)
RETURN sgd, sgd1
Nice you caught the issue. For future use, you can use an apoc procedure to create a relationship that allows you too define the relationships dynamically.
Another approach that would have worked for you is using the apoc.do.when procedure that allows you to implement an if/else statement, conditionally executing two blocks of cypher.