The problem of uniqueness and indexing restrictions

I have to do something wrong or make mistakes regarding restrictions and indexing. I have the following:

CREATE CONSTRAINT ON (u:User) ASSERT u.user_id IS UNIQUE

and

CREATE INDEX ON :User(user_id)

I tried changing the order, but regardless of what I get:

Neo.ClientError.Schema.ConstraintAlreadyExists

or

Neo.ClientError.Schema.IndexAlreadyExists

depending on the order.

I do not understand why I could not do this. I want search queries to be fast for user_id, so I am indexing, and I also want to make sure it user_idis unique, so I have a limitation.

What? I do not understand? How should I do it?

+4
source share
3 answers

Adding a unique constraint will also add an index to this property, so a single constraint is sufficient.

. http://docs.neo4j.org/chunked/stable/query-constraints.html

" , , . Cypher , . - , ."

+6

, .

To make it user_idunique and indexed, you should use this:

CREATE CONSTRAINT ON (user:User) ASSERT user.user_id IS UNIQUE

>>> Added 1 constraint, returned 0 rows in 107 ms

To verify that the index has been added correctly:

:SCHEMA

>>> Indexes
>>>   ON :User(user_id)             ONLINE (for uniqueness constraint) 
>>> 
>>> Constraints
>>>   ON (user:User) ASSERT user.user_id IS UNIQUE

Please note that if you try to delete an index that was automatically created using the uniqueness constraint, it will fail:

DROP INDEX ON :User(user_id)

>>> Unable to drop index on :User(user_id): Index belongs to constraint: :User(user_id)

The correct way to remove this is to remove the uniqueness constraint:

DROP CONSTRAINT ON (user:User) ASSERT user.user_id IS UNIQUE

>>> Removed 1 constraint, returned 0 rows in 108 ms
0
source

All Articles