In neo4j, can one node and another node have multiple relationships of the same type?

I have a model for a teacher to offer a course. We know that a teacher can open a course in different semesters, so how do I create the relationship of the same type but different semester attributes between the teacher node and the course node ?

You can model this in more than one way and the model you chose will depend on the queries.

  1. You can have a separate node for CourseInstance, based upon the semester.
  2. You can have a property on the TEACHES_COURSE relationship that specifies the semester.
  3. You can use a specific relationship name such as TEACHES_COURSE_2019-01

You definitely do NOT want to create the same relationship between two nodes unless they differ by a property on the relationship. Using the specific relationship will most likely perform better for you, but specializing the CourseInstance may also perform well. You need to try the model that will give you the best performance for the queries.

Elaine

1 Like

Thank your for your efficient reply ! I'm having a try, I will reply you again if I encounter other problems.Thanks again.:smile:

@elaine_rosenber wanted to reopen this to discuss more on:

You definitely do NOT want to create the same relationship between two nodes unless they differ by a property on the relationship. Using the specific relationship will most likely perform better for you, but specializing the CourseInstance may also perform well.

Can you share why this is the case?

An example where this might apply is: User's photos. A user can have thousands of photos but only 1 current profile picture. Based on that, there could be two relationships:

(:User) - [:HAS_PHOTO] -> (:Photo)
(:User) - [:WITH_PROFILE_PHOTO] -> (:Photo)

You can also model a boolean property: "profile" but this will exist on every single relationship but true only for one and false for every other relationship. Even if this is indexed, this would perhaps be slower for a query like:

match (u: User) - [:HAS_PHOTO {profile: true}] -> [p: Photo]
return u, p

vs

match (u: User) - [:WITH_PROFILE_PHOTO] -> [p: Photo]
return u, p

Is the above (^) correct?