Force relationships from a node label X to a node label Y to have specific properties

constraint
schema
(Telmo Trooper) #1

I've made this same question in StackOverflow, but I'm asking here as well to increase the odds of getting some help.

I'm aware that using the "relationship property existence constraint" available in Neo4j Enterprise I can force relationships of a given label to have some properties, for example:

(Relationships of label "LIKED" always have a "day" property)

CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)

But is it possible to force relationships from a node label X to a node label Y to have specific properties? An example of what I would like to do would be:

(Relationships of label "BOUGHT" from/to nodes label "Person" to/from nodes label "Book" always have a "day" property)

CREATE CONSTRAINT ON (p:Person)-[bo:BOUGHT]-(b:Book) ASSERT exists(b.day)

But that isn't considered a valid Cypher command. Is there a way to achieve this effect at a schema level or would I need to implement it in the application layer? Thank you.

0 Likes

(Michael Porter) #2

Hey welcome to the community! Right now there there's no way to ensure those constraints exist via the neo4j schema you'd have to figure out a way to do that in your application layer.

0 Likes

(Neo4j) #3

Michael,
I think with triggers you can do it right in Neo4j. Read David Allen's response to a question on triggers: Triggers -- matching vs filtering on trigger criteria

I believe you can create a trigger which upon creation of a relationship of type RELTYPE will kick off arbitrary code that you write, which can check the nodes it connects to and based on their types you can make changes to any of the 3 objects connected. They are all within scope.
Joe

0 Likes

(Telmo Trooper) #4

Thank you guys for your input, I'll be sure to try and implement a solution based on triggers, as @neo4joe suggested. It seems to be the best alternative for dealing with this kind of requirement outside the application layer. I'll post my results here once I've attempted writing the solution.

0 Likes