Search code examples
neo4jneomodel

Neomodel: unique_index not working on StringProperty()


I have a company node where the name should be unique:

class Company(StructuredNode):
    name = StringProperty(unique_index=True)
    participated = ArrayProperty(StringProperty())

However, when I run the code TWICE to create the company, it creates two nodes with the exact same name:

from neomodel import db
import graph_db
from graph_db.model import Company, Employee, Position, Department, Metric

with db.write_transaction:
    acme_inc = Company(name="Acme Inc", participated=[2021]).save()

Two Nodes with Identical Names, Despite Unique_Index on Name

Is there a reason that this constraint isn't being enforced?

Thanks!


Solution

  • unique_index=True is the correct field to use.

    I had the same issue, it was caused by not having the indexing and constraints instructions installed as explained here: https://neomodel.readthedocs.io/en/latest/configuration.html#enable-automatic-index-and-constraint-creation

    from neomodel import install_labels
    install_labels(Company)