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.

Duplicate relationships

munshine
Node Clone

Hello,

Just starting today to read about Neo4j, I'm already loving it. But this is the very begining of my journey, and I'm wondering very simple questions. I hope I didn't miss the obvious answer in the documentation.

Is it possible to have several output relationships of the same type ?

Like in this diagram, is is legal to have several "HAS_VERSION" relationships coming from the same entity ?
2X_e_e121d27593e60c49b41a1fb66eae89511579e75a.png

Thanks a lot !

1 ACCEPTED SOLUTION

Hey munshine, welcome to Neo4j!

Yes, it is possible to have relationships of the same type from a single node. In fact, this is going to be how your graphs are modelled most of the time as it describes the relationship between your nodes. In this case, your Component and Version nodes in a one-to-many relationship.

The following cypher query will create a graph similar to your example

CREATE (c:Component)
CREATE (v1:Version {version: 'v001'})
CREATE (v2:Version {version: 'v002'})
CREATE (c)-[:HAS_VERSION]->(v1)
CREATE (c)-[:HAS_VERSION]->(v2)

...and to return the graph that you've created

MATCH p = (c:Component)-[:HAS_VERSION]->(v:Version)
RETURN p LIMIT 10

Just be careful of the possibility of duplicate nodes. You might want to use MERGE in the place of CREATE to avoid creating duplicate Version nodes if it is unintended.

This documentation might help you get going

View solution in original post

2 REPLIES 2

Hey munshine, welcome to Neo4j!

Yes, it is possible to have relationships of the same type from a single node. In fact, this is going to be how your graphs are modelled most of the time as it describes the relationship between your nodes. In this case, your Component and Version nodes in a one-to-many relationship.

The following cypher query will create a graph similar to your example

CREATE (c:Component)
CREATE (v1:Version {version: 'v001'})
CREATE (v2:Version {version: 'v002'})
CREATE (c)-[:HAS_VERSION]->(v1)
CREATE (c)-[:HAS_VERSION]->(v2)

...and to return the graph that you've created

MATCH p = (c:Component)-[:HAS_VERSION]->(v:Version)
RETURN p LIMIT 10

Just be careful of the possibility of duplicate nodes. You might want to use MERGE in the place of CREATE to avoid creating duplicate Version nodes if it is unintended.

This documentation might help you get going

Thank you very much for the clear answer 🙂