Search code examples
neo4jcypher

Is there a way to have neo4j / cypher place constraint on a nested property?


I want to put a uniqueness constraint on value.path, how can I do it ?

{
  "identity": 16866,
  "labels": [
    "Component"
  ],
  "properties": {
    "asset_id": "",
    "id": "ComponentIdentity",
    "version": "#head",
    "value": "{
            "id": "f75cb59f-85ce-4247-979a-c8c8784b98d4",
            "path": "interactions.json"
        }"
  },
  "elementId": "16866"
}

I have tried

CREATE CONSTRAINT ComponentPathUniqueness IF NOT EXISTS
FOR (n:Component)
REQUIRE n.value.path IS NODE UNIQUE

But it fails with Invalid input '.'


Solution

  • Persisting nested properties is not supported by Neo4j so what you describe is not feasible.

    Instead of nesting properties, you can define an extra node and link it to the original node with a relationship. Alternatively, you can "flatten" the nested properties to a non-nested dictionary of properties.