Create relationship based on column value from CSV

Hi All ,

I am facing issue while creating nodes based on column values

Snippet of csv file is below.

med_name , dis_name
medA , diseaseA
medB, diseaseA
medC, diseaseB

Here I want to create nodes which will have relationship
medicine->[IS_MEDICINE_FOR]->disease
In above case 2 nodes medA and medB will have relationship with diseaseA node and medC will have separate relationship with diseaseB.
I tried to below query but it create different separate relationship for each medA->diseaseA as one pair and medB->diseaseA as one pair but I want node of diseaseA to be get created only once.

Below is query I have tried.I also tried merge but nothing worked.

load csv with headers from "file:///med_dis.csv" as row
    create (m1:Medicines{label:row.med_name})-[r:IS_MEDICINE_FOR]->(d1:Diseases{label:row.dis_name})

Any help for same will be much appreciated.

Try using ‘merge’ instead of ‘create’

Hello @prajaktanikam14

I'm tired a bit and I'm going to bed soon but I know how much it helps to have a detailed answer quickly. Merge is preventing to create the same node, path or relationship based on one or more properties. The way it works EVERYTHING must match to avoid creating an other entity or path.

If you merge the entire path for each row of your csv file, each row of your file and in this case each path merged will be unique so merge will always create a new path with the start node, the relation and the end node.

So you must merge them individually for each row of your csv file.

load csv with headers from "file:///med_dis.csv" as row
MERGE (m:Medecine {name: row.med_name})
MERGE (d:Disease {name: row.dis_name})
MERGE (m)-[:IS_MEDICINE_FOR]->(d)

Nice project by the way, are you student in medicine?

1 Like

Hello @tard_gabriel

Thank you for response and such a detailed answer! I really appreciate it :slightly_smiling_face:

I am not student in medicine but doing some project related to medicine domain.