Alexander McFarlane
Alexander McFarlane

Reputation: 11293

Neo4j Design: When to use Properties for Relationships

What is the proper case for using the Property attribute of a Relationship in Neo4j? (Including examples of when and when not to use them)


Example

Consider a computer used by Team A and Team B, where each team have their own internal value id for the computer:

Node = Team : properties = {'name'='Team A'}
^
|
Relationship = "Used By..."
|
Node = Computer : properties = {'Type':'MacBook', 'CPU':'i7', 'id'='TeamA1-MBKi7'}

If Team B has the same relationship but with a different value for id:

Would it be correct to move the property id out of the Computer node and into a relationship property?

e.g.

Node = Team : properties = {'name'='Team A'}
^
|
Relationship = "Used By..." : properties = {'id'='TeamA1-MBKi7'}
|
Node = Computer : properties = {'Type':'MacBook', 'CPU':'i7'}

Upvotes: 3

Views: 220

Answers (1)

cybersam
cybersam

Reputation: 66967

To address your specific example, you could add a new Model node label to describe a type of computer, as in:

(:Team {name: 'Team A'})<-[:USED_BY]-(:Computer {id: 'TeamA1-MBKi7'})-[:IS_A]->(:Model {type:'MacBook', cpu:'i7'})

That will allow multiple Computer nodes to share the same model information.

To address your more general question, here are some thoughts:

  1. Neo4j currently does not allow you to create an index or uniqueness constraint on a relationship. (Remember, for nodes: an index or uniqueness constraint is always associated with a node label AND a node property.) Therefore, if you wanted to (or might want to) create an index or a uniqueness constraint involving a property, you should put it in a node.
  2. A relationship can only be used once, to make a single connection between 2 nodes. If you wanted to (or might ever want to) involve the same property value in multiple connections, you should consider putting it in a node.
  3. If a property is always relevant to a specific pair of nodes (and is not relevant to just one of those nodes), then it should be a relationship property.

Upvotes: 4

Related Questions