Creating a relationship to a relationship in Neo4j

Viewed 1025

Is it possible to create a relationship to a relationship in Neo4j?

The use-case goes something like this:

  • I have a bunch of questions, like "What movie should we see?"
  • Each question can have many options like "Movie1", "Movie2", etc.
  • For each question I want a user to be able to vote for their favorite option.

The graph would preferably look something like this:

(:Question {name:"What movie?"})-[:Option]->(:Movie {name:"Movie1"})
                                     ^   
                                     |
                                  [:Vote]
                                     |
                                  (:User)

I realize that one way I could solve this is with the following:

(:Question)-[:Option]->(:Movie)<-[:Vote]-(:User)

But then if I decide to remove the Movie as an Option in the future, I don't get to take advantage of DETACH and will have to manage removing the Vote relationship myself. Not to mention, if the Movie belongs to multiple categories, I have to keep track of which Question->Movie relationship it belongs to (probably with some sort of ID). It just seems very messy...

Is is possible to create a relationship to a relationship? Or am I going to have to manually enforce referential integrity?

2 Answers

I'm investigating this. The only reasonable way I can find is to assign an identifier to the relationship (:HAS in your case) and then use it in the pointing relationship (:VOTE).

Neo4j has internal IDs for this (see the ID() function), but I usually prefer to try to assign a semantically meaningful ID (like a person's national insurance number, a page URL or, in case of relations, the hash code of its concatenated endpoint's identifers).

Related