# Facing problem to create relationship between two different node from csv

**URL:** <https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241>\
**Category:** Cypher\
**Created:** [July 7, 2020, 8:22pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241 "2020-07-07T20:22:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalyan\_b\_aninda](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kalyan_b_aninda/32/6079_2.png) [@kalyan\_b\_aninda](https://community.neo4j.com/u/kalyan_b_aninda)\
**Post date:** [July 7, 2020, 8:22pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/1 "2020-07-07T20:22:18Z")

</div>

Hello, For testing purpose I am trying to make relationship two nodes . One node has Customer label and other node as Device label

Node creation is done. I am. facing problem to make relationship between two nodes

```auto
RELATIONSHIP,IDENTITY_ID,DEVICE_ID
HAS_DEVICE,22q,1010k
HAS_DEVICE,q22,1020k
HAS_DEVICE,ac23,1030k
HAS_DEVICE,dfd,1050k
HAS_DEVICE,22q,1070k
HAS_DEVICE,ac23,1090k

```

This is my relationship device.csv file . The identity\_id values exists in the CUSTOMER labeled nodes, and DEVICE\_ID values exist in DEVICE labelled node

```auto
|IDENTITY_ID|DIVISION|
|---|---|
|22q|KHULNA|
|q22|DHAKA|
|ac23|RANGPUR|
|dfd|KHULNA|
|123g|MYMENSINGH|
|eo89|CHITTAGONG|
|ere3|SYLHET|
|lo1o3|DHAKA|
|90de|CHITTAGONG|
|er02|CHITTAGONG|
|22q|KHULNA|

```

```auto
|DEVICE_ID|DEVICE_NAME|
|---|---|
|1010k|SAMSUNG|
|1020k|APPLE|
|1030k|SONY|
|1050k|OPPO|
|1070k|ONEPLUS|
|1090k|SYMPHONY|
|1080k|NOKIA|
|1015k|ZTE|
|1025k|XIAOMI|
|1030k|XIAOMI pro|

```

so these are my values of customer.csv and device.csv

so the idea is I will create relationship between two nodes based on IDENTITY\_ID and DEVICE\_ID in relationship files.  
I have tried several way to do this

since it is test case but this code will be applied in big size of data

```auto
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER), (s:DEVICE)
WHERE f.IDENTITY_ID = row.IDENTITY_ID
AND s.DEVICE_ID = row.DEVICE_ID
CALL apoc.create.relationship(f, row.RELATIONSHIP,{}, s) YIELD rel
RETURN rel

```

using apoc library

CALL apoc.periodic.iterate('  
load csv with headers from "file:///relationship\_device.csv" AS row return row ','  
MATCH (c:CUSTOMER {IDENTITY\_ID: row.IDENTITY\_ID})  
MATCH (d:DEVICE{DEVICE\_ID: row.DEVICE\_ID})  
MERGE(c)-[r:HAS\_DEVICE]-\>(d)

```
                     ',{batchSize:1000, iterateList:true, parallel:true})

```

```auto
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS line
MATCH (customer:CUSTOMER {IDENTITY_ID:line.IDENTITY_ID}),(device:DEVICE {DEVICE_ID:line.DEVICE_ID})
CREATE (customer)-[:HAS_DEVICE{RELATIONSHIP: line.RELATIONSHIP}]->(device)

```

so far no relationship is created by this two nodes.What thing I am missing here.Using apoc solution will be good for future development. Any help will be highly appreciated

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [July 7, 2020, 8:38pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/2 "2020-07-07T20:38:50Z")

</div>

Hello @kalyan_b_aninda 🙂

Can you try this?

```auto
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MATCH (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

```

Can you show us how are the nodes in your database? (which properties and labels) 🙂

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![kalyan\_b\_aninda](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kalyan_b_aninda/32/6079_2.png) [@kalyan\_b\_aninda](https://community.neo4j.com/u/kalyan_b_aninda)\
**Post date:** [July 7, 2020, 8:50pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/3 "2020-07-07T20:50:42Z")

</div>

Thanks you for your reply .it is showing after query  
(no changes, no records)

MATCH (n:CUSTOMER) RETURN n [Table view]  
{  
"IDENTITY\_ID": "22q",  
"DIVISION": "KHULNA"

}

{  
"IDENTITY\_ID": "q22",  
"DIVISION": "DHAKA"  
}

MATCH (n:DEVICE) RETURN n [Table view]

{  
"DEVICE\_ID": "1010k",  
"DEVICE\_NAME": "SAMSUNG",  
}

and so. on ....

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [July 7, 2020, 8:56pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/4 "2020-07-07T20:56:12Z")

</div>

How did you create your nodes?  
Which version of Neo4j are you using?  
How many rows do there is in the file?

Try with `MERGE` instead of `MATCH`:

```auto
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MERGE (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MERGE (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

```

---

<div class="post-metadata">

**Author:** ![kalyan\_b\_aninda](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kalyan_b_aninda/32/6079_2.png) [@kalyan\_b\_aninda](https://community.neo4j.com/u/kalyan_b_aninda)\
**Post date:** [July 7, 2020, 9:03pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/5 "2020-07-07T21:03:11Z")

</div>

current version is 3.5.8 in my pc. didnt update it yet. I have posted whole rows in my first post. but it will increase later.just doing a POC with these dummy rows.

`CALL apoc.periodic.iterate('CALL apoc.load.csv("file:///customer_account.csv") yield map as row return row','CREATE (p:CUSTOMER) SET p = row', {batchSize:1000000, iterateList:true, parallel:true})`

This is my actually node creation query

and I tried your recent given query here.  
it is showing

```auto
Neo.ClientError.Statement.SyntaxError: WITH is required between MERGE and CALL (line 4, column 1 (offset: 158))
"CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel"

```

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [July 7, 2020, 9:08pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/6 "2020-07-07T21:08:21Z")

</div>

You should update to last version of Neo4j 🙂

To create your nodes, use:

```auto
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///customer_account.csv" AS row
MERGE (p:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
SET p += row

```

To create relationships:

```auto
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///relationship_device.csv" AS row
MATCH (f:CUSTOMER{IDENTITY_ID:row.IDENTITY_ID})
MATCH (s:DEVICE{DEVICE_ID:row.DEVICE_ID})
CALL apoc.create.relationship(f, row.RELATIONSHIP, {}, s) YIELD rel
RETURN rel

```

Did you put `UNIQUE CONSTRAINTS` on `IDENTITY_ID` and `DEVICE_ID`?

I also encourage you to follow [the naming rules](https://neo4j.com/docs/cypher-manual/current/syntax/naming/) 🙂

---

<div class="post-metadata">

**Author:** ![kalyan\_b\_aninda](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kalyan_b_aninda/32/6079_2.png) [@kalyan\_b\_aninda](https://community.neo4j.com/u/kalyan_b_aninda)\
**Post date:** [July 7, 2020, 9:23pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/7 "2020-07-07T21:23:41Z")

</div>

Thanks it worked. Yeh constraint will be needed for performance optimisation. I had bad experience before without giving constraint .It took too much time to create nodes and relationship for big volumes of data. then apoc module and setting constraint on id gave me relief.

A question to ask for knowledge gaining. apoc has parallel feature. so it runs parallelism for optimising query performance. Does this Using Periodic commit works for big volume data processing. I am not good at neo4j yet but learning :)

and question number 2 can you give a short explanation for this Node creation and relationship. like my previous code worked before apart from this relationship code

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [July 7, 2020, 10:21pm UTC](https://community.neo4j.com/t/facing-problem-to-create-relationship-between-two-different-node-from-csv/21241/8 "2020-07-07T22:21:12Z")

</div>

Yeah `Periodic commit` works for big volume data processing 🙂, it's done for that.

To be honest, I don't really know where was the bug since your code looked good, that's why I asked you to do a fresh start with a new database and a clean query 🙂
