cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Relationship one to many

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:

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

2 REPLIES 2

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 https://neo4j.com/developer/kb/understanding-how-merge-works/). If you are importing relational data and the nodes already exist, have you tried:

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

intouch_vivek
Graph Steward

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