question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why are properties with unique index required?

See original GitHub issue

Hello 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:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
bhalbertcommented, Aug 30, 2017

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

image

6reactions
jonadalycommented, Aug 16, 2019

@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/

Unique property constraints ensure that property values are unique for all nodes with a specific label. Unique constraints do not mean that all nodes have to have a unique value for the properties — nodes without the property are not subject to this rule.

As @yarmash comments above, the solution is very simple. I am happy to raise a PR for this change if it would help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unique and non-unique indexes - IBM
Unique indexes are indexes that help maintain data integrity by ensuring that no rows of data in a table have identical key values....
Read more >
Difference between Unique Indexes and Unique Constraints ...
We use Constraints for the Consistency property of an ACID. It means that only valid data that satisfies the condition should exist in...
Read more >
SQL Server Unique Index: Everything you need to know -
A Unique Index is placed on a column to guarantee there will be no duplicate values within that column. Each value in that...
Read more >
Create and use an index to improve performance
If you create a unique index, Access doesn't allow you to enter a new value in the field if that value already exists...
Read more >
Unique Indexes - Marten DB
Unique Indexes are used to enforce uniqueness of property value. They can be combined to handle also uniqueness of multiple properties. Marten supports...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found