简体   繁体   English

令人困惑的新模型`unique=True`行为

[英]Confusing neomodels `unique=True` behaviour

The behaviour of the unique property on neomodel.StructuredNode is a bit confusing to me. neomodel.StructuredNodeunique属性的行为让我有点困惑。

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.并非所有的Person实例都有这个“legacy_external_id”,但是它存在的地方必须是唯一的。

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 .首先,它应该是unique_index=True ,而不是unique=True

Also, the current version of Neomodel does not support the combination of unique_index=True and required=False .此外,当前版本的 Neomodel 不支持unique_index=Truerequired=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).如果一个属性被设置为唯一的,那么它也会被自动视为需要(大概是因为否则,你可能有两个或多个具有 null 属性的节点,这不会是唯一的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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