简体   繁体   中英

Confusing neomodels `unique=True` behaviour

The behaviour of the unique property on neomodel.StructuredNode is a bit confusing to me.

Can anyone point me to the docs where this is explained? Or let me know what I'm misunderstanding?

The model:

class Person(neomodel.StructuredNode):

    uuid = neomodel.UniqueIdProperty()

    legacy_external_id = neomodel.StringProperty(unique=True, required=False)

Not all Person instances will have this "legacy_external_id", but nonetheless where it exists it needs to be unique.

The above code seems to me the to fit that description.

Yet in practice the output is:

In[]: p = Person(legacy_external_id='u12345')
In[]: p.save()
Out[]: < Person: {'uuid': '7d7e5c2224d647e9a87d23b9cb1c4153', 'legacy_external_id': 'u12345'} >

In[]: p = Person(legacy_external_id='u12345')
In[]: p.save()
Out[]: < Person: {'uuid': 'ed7690f8f28b4cf288c35d2130a2e6e4', 'legacy_external_id': 'u12345'} >

In[]: p = Person(legacy_external_id='u12345')
In[]: p.save()
Out[]: < Person: {'uuid': '7393a4b5022d40d8aca002cecac9b124', 'legacy_external_id': 'u12345'} >

Just merrily proliferating nodes (without error or warning) is not the behaviour I expected.

I'm fine to write my own validation, but before I ploughed ahead to do that I'm interested to understand my miscomprehension.

Any advice is appreciated, thank you!

First, it should be unique_index=True , not unique=True .

Also, the current version of Neomodel does not support the combination of unique_index=True and required=False . If a property is set as unique, then it is automatically treated as required as well (presumably because otherwise, you could have two or more Nodes with null property, which wouldn't be unique).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM