Creating cities in Neo4j

We will model cities as nodes, as shown in Figure 2.4, with the city's name and country as properties. Cities should have unique names. For this, we can add a constraint before we start creating cities in our graph.

The query is as follows:

neo4j-sh (?)$ CREATE CONSTRAINT ON (city: City) ASSERT city.name IS UNIQUE;

The output of the preceding query is as shown:

+-------------------+
| No data returned. |
+-------------------+
Constraints added: 1

This adds a constraint on all nodes that will henceforth be created with the label City to have a unique name property. The city in (city: City) is a placeholder, like a variable, for any node with label City. Note that the addition of a uniqueness constraint is idempotent—it can be ...

Get Neo4j Graph Data Modeling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.