Creating Existence Constraints for Relationship Properties

Property must exist

In our Movie data model, we want to ensure every RATED relationship has values for the rated and timestamp properties. In the previous lesson, you created the existence constraint for the rating property for the RATED relationships in the graph. Create the existence constraint for the timestamp property of the RATED relationships.

In the sandbox on the right, modify the code to create this existence constraints in the graph:

  • constraint_name: RATED_timestamp_exists

  • relationship type: RATED

  • property_key: timestamp

After you have created the constraint, list all of them:

cypher
SHOW CONSTRAINTS

If you create an incorrect constraint with different names or property_key names, do not worry. You can create new ones, provided the constraint_name or property_key is unique. Later in this course you will learn how to remove constraints from the graph.

If you reload this page, the graph will be reset to what it should be at the beginning of the Challenge.

Try removing the timestamp value for these relationships. It should return an error:

cypher
MATCH (User {userId: "1"})-[x]-()
SET x.timestamp = null

Validate Results

Once you have created the constraint, click the Check Constraints button and we will check the database for you.

Hint

Constraint names, relationship types, and property key names are all case-sensitive.

The pattern you should specify for creating the constraint is:

()-[x:RATED]-()

If you mis-specify a relationship type or property key, the constraint will not be created.

You can type SHOW CONSTRAINTS after you have created each constraint.

If you mess up, you can reload this Challenge page and you should be where you need to be at the beginning of this Challenge.

Solution

Here is the statement to create the constraint:

cypher
CREATE CONSTRAINT RATED_timestamp_exists IF NOT EXISTS FOR ()-[x:RATED]-() REQUIRE x.timestamp IS NOT NULL

Summary

In this challenge, you demonstrated that you can create existence constraints a relationship property in the graph. In the next lesson, you will learn how to create Node key constraints.