2
votes

I'm new in Neo4j an I need some help.

I'm trying to make constraint in multiple properties of nodes at once per two meanings:

  1. I need to specify as constraint many properties without typing again and again all over the properties with the command
  2. I need to define many properties as ONE- UNITY constraint like in SQL when 3 attributes is a primary key and not separably.

How can I achieve it?

1

1 Answers

2
votes

You are actually asking 2 questions.

  1. The APOC procedure apoc.schema.assert is helpful for conveniently ensuring that the DB has the required set of indexes and constraints. (Be aware, though, that this procedure will drop any existing indexes and constraints not specified in the call.)

    For example, as shown in the documentation, this call:

    CALL apoc.schema.assert(
      {Track:['title','length']},
      {Artist:['name'],Track:['id'],Genre:['name']});
    

    will return a result like this (also, if an index or constraint had been dropped, a row with the action value of "DROPPED" would have been returned as well):

      ╒════════════╤═══════╤══════╤═══════╕
      │label       │key    │unique│action │
      ╞════════════╪═══════╪══════╪═══════╡
      │Track       │title  │false │CREATED│
      ├────────────┼───────┼──────┼───────┤
      │Track       │length │false │CREATED│
      ├────────────┼───────┼──────┼───────┤
      │Artist      │name   │true  │CREATED│
      ├────────────┼───────┼──────┼───────┤
      │Genre       │name   │true  │CREATED│
      ├────────────┼───────┼──────┼───────┤
      │Track       │idtrue  │CREATED│
      └────────────┴───────┴──────┴───────┘
    
  2. Since there is not (yet) any way to create an index or constraint on multiple properties of a node label, one popular workaround is to use an extra property whose value is an array of the values you want to use. You will have to make sure the values are all of the same type, converting some if necessary. Unfortunately, this requires storing some data redundantly, and makes your code a bit more complex.