Skip to main content
Unique indexes allow you to enforce uniqueness constraints on your data, ensuring that no two records share the same value for a given field or edge relationship.

Unique Index on Node Fields

To enforce uniqueness on a node field, use the UNIQUE INDEX keywords before the field name:
This creates a unique secondary index on the email field, preventing duplicate email addresses across all User nodes.

Syntax

Combining with Default Values

You can combine unique indexes with default values:

Querying by Unique Index

Nodes with unique indexes can be queried using object syntax, just like regular indexes:

Unique Edges

Unique edges ensure that only one edge of a given type can exist between two specific nodes. This is useful for relationships that should be singular, such as “best friend” or “primary contact”.

Syntax

The UNIQUE modifier is placed between the edge name and the edge body.

Example

With this schema, each user can only have one BestFriend edge pointing to another user. Attempting to create a second BestFriend edge from the same user will enforce the uniqueness constraint.

Unique Edges with Properties

Unique edges can also have properties:

Complete Example

In this example:
  • Each employee has a unique employee_id and email
  • Each project has a unique project_code
  • Each project can only have one lead (via the UNIQUE edge constraint)
  • Employees can work on multiple projects (non-unique WorksOn edge)

Constraint Violations

When inserting data that violates a unique constraint, HelixDB will return an error. This applies to both unique node field indexes and unique edges.

Node Field Violations

If you attempt to insert a node with a value that already exists for a unique indexed field, the operation will fail:
The second operation will return an error indicating that a node with that email already exists.

Edge Violations

Similarly, attempting to create a duplicate unique edge will fail:
Use unique constraints to enforce data integrity at the database level. Handle the returned errors in your application to provide appropriate feedback to users, such as “This email is already registered.”