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.

How do I create a single, two way relationship and attach properties to the realationship?

I am a complete beginner so forgive this simple question.

I have database which contains nodes for three people, a man, his wife and his sister.

  1. I would like to show a filial relationship between the brother and sister which has arrows at both ends, i.e. there is no hierarchical assumption, a man is not brother to a woman.
  2. I would like to show a similar marital relationship, where there is an arrow at both ends. On this relationship however I would like to attach a date range, from and to.

I don't really want to have two separate relationships for this if at all possible.

The code I have used is as follows
MATCH
(a:Person),
(b:Person)
WHERE a.name = 'Smith, Joseph' AND b.name = 'Jones, Dorothy'
CREATE (a)-[r:Marital {name: a.name + '<->' + b.name}]->(b)
MATCH
(c:Person),
(d:Person)
WHERE c.name = 'Smith, Joseph' AND d.name = 'Smith, Sarah'
CREATE (c)-[r:Filial {name: c.name + '<->' + d.name}]->(d)

Thanks so much!

4 REPLIES 4

martin3
Node Clone

Welcome Botanic Macaroni!

In neo4j, relationships are created with, and always have one and only one direction. It is possible to create two or more relationships between two nodes, and the same type of relationship can have opposite directions. To have two relationships of similar typ between two nodes is often unnecessary and is then often not good practise.

It is a often good practice to use verbs for naming relationships.

There are many ways to create and name these two kind of relations between people.

One alternative could be to use a "IS_A_SIBLING_OF" relationship between brothers and sisters, and it might also be possible to use a "IS_MARRIED_TO" between husband and wife.

When you try to MATCH these kinds of relationships, then you don't need to specify the direction.

You know that if A is a sibling of B, then B will also be a sibling of A. And if B is married to C, then C will also be married to B. So in this case the directions of the relationships does not matter.

To sum up:

In neo4j, you always need to specify a direction when you create relationship. In this case the created direction could be arbitrary. It doesn't matter which direction you choose.

In neo4j, you do not need to specify a direction when you match relationship. In that case you will match the existence of a relationship type regardless of the direction of the relationship type.

Good Yule!

Thank you so much for this, hugely helpful to know that I am not looking for something that doesn't happen!
I don's suppose that you know whether I can add attributes to the relationship, like the marriage dates?
Thanks again, Happy Christmas!

Yes, a relationship can have properties in key-value pairs, like: {married: YYYY-MM-DD}.

Thank you again, I think I can make some good progress now.