Why are properties with unique index required?
See original GitHub issueHello I have a problem with unique index properties being required.
I have a class that is essentially:
class Class(neomodel.StructuredNode)
title = neomodel.StringProperty(required = True)
fileName = neomodel.StringProperty(required = True, unique_index = True)
id = neomodel.IntegerProperty(required = True)
otherProperty = neomodel.StringProperty(required = False)
anotherProperty = neomodel.StringProperty(required = False)
otherId = neomodel.StringProperty( required = False, unique_index = True )
Note the otherId that is not required, but should be unique. When I run an update on my script some the new data is new nodes and some of the data is fileName changes for existing nodes. I am on the fence about whether I want to update the existing nodes or to make new nodes and change the otherId on the oldNodes so they don’t conflict.
The way I was planning on doing this is making a list of dictionaries with the title, fileName, id set but not the otherProperty. Then call models.Class.get_or_create(*list)
. After which I could go update all the existing nodes to replace their otherId’s with a new string like otherId.old
.
I am having a problem with this because I get an exception for RequiredProperty(). I looked at the neomodel source code where the exception is being generated and it looks to me like it will be raised if a property with a unique_index is not set. Is this correct? Why are unique_index’s required? Why can I not call get_or_create with objects missing a property where required is false?
@classmethod
def deflate(cls, obj_props, obj=None, skip_empty=False):
# deflate dict ready to be stored
deflated = {}
for key, prop in cls.defined_properties(aliases=False, rels=False).items():
# map property name to correct database property
db_property = prop.db_property or key
if key in obj_props and obj_props[key] is not None:
deflated[db_property] = prop.deflate(obj_props[key], obj)
elif prop.has_default:
deflated[db_property] = prop.deflate(prop.default_value(), obj)
elif prop.required or prop.unique_index:
raise RequiredProperty(key, cls)
elif skip_empty is not True:
deflated[db_property] = None
return deflated
Thank you very much for your help and I hope this was clear
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
I read the documentation you linked to and I can’t find anything saying that unique properties are required.
In the example below there is a constraint for ‘uid’ to be unique. I create a node that had no ‘uid’ and return it therefore Neo4j does not require unique properties to be non-null
@robinedwards @aanastasiou Please could we reopen this issue? This behaviour of
neomodel
directly contravenes the documentation - to quote the docs linked above:https://neo4j.com/docs/cypher-manual/current/schema/constraints/
As @yarmash comments above, the solution is very simple. I am happy to raise a PR for this change if it would help?