# Relationship one to many

**URL:** <https://community.neo4j.com/t/relationship-one-to-many/12392>\
**Category:** Cypher\
**Created:** [December 1, 2019, 10:17pm UTC](https://community.neo4j.com/t/relationship-one-to-many/12392 "2019-12-01T22:17:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul.velthuis](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/paul.velthuis/32/7440_2.png) [@paul.velthuis](https://community.neo4j.com/u/paul.velthuis)\
**Post date:** [December 1, 2019, 10:17pm UTC](https://community.neo4j.com/t/relationship-one-to-many/12392/1 "2019-12-01T22:17:57Z")

</div>

Hello I am having the following Table

Name Role Project  
name1 Owner Project1  
name1 Owner Project2  
name1 Owner Project3

I currently have the query:

MATCH (p:Person {name: "name1"})--\>(n:Project)  
MERGE(p)-[:HAS\_ROLE]-\>(n)  
RETURN p,n

This leads to a output that is for every name name1 to a project like  
name1 -\> Project1  
name1 -\> Project2  
name1 -\> Project3

Thus 3 times a node name1 pointing to a project

What I would like to have is one node name1 pointing to each of the projects

A related answer that I did not completely understand is here:

> [@Neo4j query for relationship](https://community.neo4j.com/t/neo4j-query-for-relationship/2138):
>
> Hello guys, i have existing five node with same label and i want to create one other node and also create a relation with all existing five node i was try but its create one to one relation on one to many relation. please help. thanks match (e:video) create(v:videomaster{name:'videomaster'}), (e)-[r:DO\_SHOPPING\_WITH]-\>(v) see screen sort But i want to like this at creation time of node

I would be happy if somebody can show me how such a relationship works

---

<div class="post-metadata">

**Author:** ![terryfranklin82](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/terryfranklin82/32/5233_2.png) [@terryfranklin82](https://community.neo4j.com/u/terryfranklin82)\
**Post date:** [December 2, 2019, 2:20am UTC](https://community.neo4j.com/t/relationship-one-to-many/12392/2 "2019-12-02T02:20:01Z")

</div>

When you say "having the following table" do you mean tis data exists in a relational / sql database and you're trying to bring it all into Neo4j? Or you've already created :Person and :Project nodes in Neo4j, and you just need to create the relationships?

Apart from that you need to use MERGE carefully, as it can create duplicates that you don't intend (see [Understanding how MERGE works - Knowledge Base](https://neo4j.com/developer/kb/understanding-how-merge-works/)). If you are importing relational data and the nodes already exist, have you tried:

```auto
MATCH (p:Person {name: "name1"}), (n:Project)
MERGE (p)-[:HAS_ROLE]->(n)
RETURN p, n

```

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [December 2, 2019, 9:32pm UTC](https://community.neo4j.com/t/relationship-one-to-many/12392/3 "2019-12-02T21:32:14Z")

</div>

You can try apoc.refactor.mergeNodes too  
create (p:Person {name:"P1"})  
create (p:Person {name:"P1"})  
create (p:Person {name:"P1"})

create (proj:Project {Proj\_Name:"Proj1"})  
create (proj:Project {Proj\_Name:"Proj2"})  
create (proj:Project {Proj\_Name:"Proj3"})

create (p:Person{name:"P1"})-[r:HAS\_ROLE{name:"Owner"}]-\>(proj:Project{Proj\_Name:"Proj1"})  
create (p:Person{name:"P1"})-[r:HAS\_ROLE{name:"Owner"}]-\>(proj:Project{Proj\_Name:"Proj2"})  
create (p:Person{name:"P1"})-[r:HAS\_ROLE{name:"Owner"}]-\>(proj:Project{Proj\_Name:"Proj3"})

MATCH (p:Person)  
WITH p.name as Person\_Name, collect(p) as nodes  
CALL **apoc.refactor.mergeNodes** (nodes, {properties: "combine"}) YIELD node  
RETURN node
