# How to create relationship between 2 nodes in the same node labels?

**URL:** <https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296>\
**Category:** Neo4j Graph Platform\
**Tags:** load-csv\
**Created:** [April 2, 2022, 6:23am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296 "2022-04-02T06:23:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 2, 2022, 6:23am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/1 "2022-04-02T06:23:07Z")

</div>

Hello everyone!  
I'm trying to load some csv files to create nodes and relationship.  
But there is a problem I can't understand and solve.

Here is an example:  
In the file [navtech.csv] I created some 'NavTech' nodes.  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/c/3c20166a2e655feb96d9336259f340bc046167da.png)  
GPS系统 is one of them, which navtech\_id = 4.  
And you can see 全球定位系统's navtech\_id = 3.

In the file[r\_navtech\_navtech\_sameas.csv] I created some relationship.  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/e/2/e23a36e7df30968169d39c1f5d56d4c28d27951a.png)

so this is supposed to be [GPS系统 -same\_as -\> 全球定位系统]  
while the result turns out to be:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/9/d/9d29f5b27fc3557ee8afbb91465d5ddda1bc93c0.png)

I couldn't understand since I didn't create this relationship.  
and the subclass\_of in this picture is in the same situation.  
And if click this node, 全球定位系统 still didn't appear.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/7/87660c66d153de0cd846ec54ecc847d6d0a91749.png)

This happens to many nodes. The relationship I want to create between 2 different node in same label,but it became the relationship between the same 1 node.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/9/79365e96fd4f50a4bd34ee88f48d0bfed824c233.jpeg)

Followings are my operations..

```auto
CREATE CONSTRAINT constraint_nav IF NOT EXISTS ON (n:Nav) ASSERT n.nav_id IS UNIQUE;
CREATE CONSTRAINT constraint_nav_tech IF NOT EXISTS ON (n:NavTech) ASSERT n.navtech_id IS UNIQUE;
CREATE CONSTRAINT constraint_dev IF NOT EXISTS ON (n:Device) ASSERT n.dev_id IS UNIQUE;
CREATE CONSTRAINT constraint_sensor_type IF NOT EXISTS ON (n:SensorType) ASSERT n.sensortype_id IS UNIQUE;

```

* * *

create nodes.

```auto
LOAD CSV WITH HEADERS FROm 'file:///nav.csv' AS line
MERGE (n:Nav {nav_id: line.nav_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///navtech.csv' AS line
MERGE (n:NavTech {navtech_id: line.navtech_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///dev.csv' AS line
MERGE (n:Device {dev_id: line.dev_id}) SET n += line

LOAD CSV WITH HEADERS FROm 'file:///SensorType.csv' AS line
MERGE (n:SensorType {sensortype_id: line.sensortype_id}) SET n += line

```

* * *

create relationship.

```auto
LOAD CSV WITH HEADERS FROm 'file:///r_nav_dev.csv' AS line
MATCH (a:Nav {nav_id: line.nav_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_nav_navtech.csv' AS line
MATCH (a:Nav {nav_id: line.nav_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:adopt]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_dev.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_use.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_subclassof.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_sameas.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:same_as]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_sensortype_dev.csv' AS line
MATCH (a:SensorType {sensortype_id: line.sensortype_id})
MATCH (b:Device {dev_id: line.dev_id})
MERGE (b)-[:subclass_of]->(a)

```

Files:  
[navtech.txt](https://community.neo4j.com/uploads/short-url/zzA6iNf5lJvwa4CQlTCLDFTbXQH.txt) (1.2 KB)  
[r\_nav\_dev.txt](https://community.neo4j.com/uploads/short-url/cDdKXv38DKzJWWjZkD2TwoH6W3N.txt) (458 Bytes)  
[r\_nav\_navtech.txt](https://community.neo4j.com/uploads/short-url/vNDniatQkrwTQvuLqD3Qjm3N6kZ.txt) (682 Bytes)  
[dev.txt](https://community.neo4j.com/uploads/short-url/lcvvAIgU0YWPsfxV134OI7HhS63.txt) (750 Bytes)  
[nav.txt](https://community.neo4j.com/uploads/short-url/6Dm84LFRjokY4N99AuNLwJep0P1.txt) (32 Bytes)  
[r\_navtech\_navtech\_subclassof.txt](https://community.neo4j.com/uploads/short-url/qU9UEbJMGkAkln5CbR7JooWzR3h.txt) (663 Bytes)  
[r\_navtech\_navtech\_use.txt](https://community.neo4j.com/uploads/short-url/xqZUj2rxVr6G5TkuTfUzbXONOQ8.txt) (199 Bytes)  
[r\_sensortype\_dev.txt](https://community.neo4j.com/uploads/short-url/jbsJCTF7mCygEDNYygkiHSZWlvr.txt) (691 Bytes)  
[r\_navtech\_dev.txt](https://community.neo4j.com/uploads/short-url/htP1YLDvObShnRqVi3bkffi82tJ.txt) (257 Bytes)  
[r\_navtech\_navtech\_sameas.txt](https://community.neo4j.com/uploads/short-url/6C91TRgHhbtljQgT9Cd8u92KzgN.txt) (77 Bytes)  
[SensorType.txt](https://community.neo4j.com/uploads/short-url/832NBFRunEKRVMB1zHuCIPEPine.txt) (201 Bytes)

Sorry, my problem is a little troublesome. 😢

---

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 2, 2022, 6:40am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/2 "2022-04-02T06:40:29Z")

</div>

and 3 other relationship I didn't create....  
found in schema.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/8/189902936504201036a86f59c5a7a71f38a05de5.png)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [April 2, 2022, 8:44am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/3 "2022-04-02T08:44:31Z")

</div>

hi,

```auto
LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_use.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:use]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_subclassof.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV WITH HEADERS FROm 'file:///r_navtech_navtech_sameas.csv' AS line
MATCH (a:NavTech {navtech_id: line.navtech_id})
MATCH (b:NavTech {navtech_id: line.navtech_id})
MERGE (a)-[:same_as]->(b)

```

The three above blocks of code creating relations are each matching the same node in each because you are specifying the same line attribute navtech\_id in each match clause, so it is matching the same node and creating a relation between the same node.

The confusion is resulting because your two columns in your relationship-specifying files have duplicate header names. Neo4j is probably using the last value of navtech\_id that it reads as it parses each line when headings are the same. The solution is to change the headings to be unique and update your code to reflect the change.

If that is not practical, you can use indexes instead. For instance:

LOAD CSV FROM 'file:///r\_navtech\_navtech\_subclassof.csv' AS line  
With line  
Skip 1  
MATCH (a:NavTech {navtech\_id: line[0]})  
MATCH (b:NavTech {navtech\_id: line[2]})  
MERGE (a)-[:subclass\_of]-\>(b)

---

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 3, 2022, 4:44pm UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/4 "2022-04-03T16:44:31Z")

</div>

Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 3, 2022, 5:07pm UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/5 "2022-04-03T17:07:01Z")

</div>

Sorry...I find another problem.  
I use the way you wrote to load csv. And you can see the schema has 10 relationship lines,but I only create 7 relationship lines.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/b/7b4ce18c799d15dfb739231914d93e1078c19d6e.png)

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/5/3/53c86c9443a7f5a5eb39e2ee2d47c0060ff6e8c7.png)

My method is the same as that written on the first floor, only the following parts have been changed.

```auto
LOAD CSV FROM 'file:///r_navtech_navtech_use.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:use]->(b)

LOAD CSV FROM 'file:///r_navtech_navtech_subclassof.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:subclass_of]->(b)

LOAD CSV FROM 'file:///r_navtech_navtech_sameas.csv' AS line
With line
Skip 1
MATCH (a:NavTech {navtech_id: line[0]})
MATCH (b:NavTech {navtech_id: line[2]})
MERGE (a)-[:same_as]->(b)

```

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [April 3, 2022, 10:41pm UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/6 "2022-04-03T22:41:56Z")

</div>

I executed each of your imports. I did not see those relationships in the final data set. Here is a list of all the relationships that exist in the data:

match(n)-[r]-\>(m)  
return distinct([labels(n)[0], labels(m)[0], type(r)]) as distinctRelationships

 ![Screen Shot 2022-04-03 at 3.17.39 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/e/1e5ebe8c34dd8b1bbd346bb5fe6c55699353c258.png)

I also got back the same schema graph. Those relationships you highlighted do not show up in my data either. I don't know why the schema shows them.

---

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 4, 2022, 10:53am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/7 "2022-04-04T10:53:25Z")

</div>

Well, that's cofusing...

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/b/e/bee8271c61499d096131a85e7194c3d10bbc1571.png)  
I also show 7 relationships here.  
But the schema show 10 relationships...  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/b/c/bc6c57a6191544d8222c3318ac8092518664f89c.png)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [April 4, 2022, 5:45pm UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/8 "2022-04-04T17:45:33Z")

</div>

I am puzzled as well. Maybe a staff member can help.

---

<div class="post-metadata">

**Author:** ![11li](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/11li/32/5767_2.png) [@11li](https://community.neo4j.com/u/11li)\
**Post date:** [April 5, 2022, 1:51am UTC](https://community.neo4j.com/t/how-to-create-relationship-between-2-nodes-in-the-same-node-labels/54296/9 "2022-04-05T01:51:19Z")

</div>

Could you please tell me how can I get help from a staff member?
