neo4j-contrib / neomodel

An Object Graph Mapper (OGM) for the Neo4j graph database.
https://neomodel.readthedocs.io
MIT License
936 stars 231 forks source link

Structured Node `__eq__` works incorrectly for non-persisted nodes. #778

Closed OlehChyhyryn closed 4 months ago

OlehChyhyryn commented 7 months ago

Expected Behavior (Mandatory)

If nodes are not created in the database and do not have an element_id property, they should be compared by id in memory or always count as different.

Actual Behavior (Mandatory)

All non-persisted node countries as equal

How to Reproduce the Problem

Create any StructuredNode objects without saving them to the database. Call comparison.

Simple Example

from neomodel import StructuredNode

class Foo(StructuredNode):
    pass

a = Foo()
b = Foo()

print(a == b)

The problem comes from the StructuredNode.__eq__ function.

    def __eq__(self, other):
        if not isinstance(other, (StructuredNode,)):
            return False
        if hasattr(self, "element_id") and hasattr(other, "element_id"):
            return self.element_id == other.element_id
        return False

I haven't prepared a pull request because I'm unsure which behavior should be selected as a default: all non-persisted nodes. Are they not equal, or do we want to compare them by memory ID or the other way around?

Screenshots (where it's possible)

Specifications (Mandatory)

Currently used versions

Versions