neomodel icon indicating copy to clipboard operation
neomodel copied to clipboard

Using "id" as a property name leads to unexpected error

Open stellasia opened this issue 2 years ago • 1 comments

So, from the issues and responses I've found here, I understand we can't use "id" as a property name, but:

  1. The documentation only mention reserved keywords that starst with an underscore (https://neomodel.readthedocs.io/en/latest/properties.html#naming-convention) (or there is another section elsewhere I was not able to find?)

  2. The error is not clear enough about the source of the problem, Current behavior is:

    1. I can create the class
    2. I can create a node with custom "id" property and the "id" is set properly in Neo4j
    3. It fails when I try to "get" node with a weird error, which I interpreted as "neomodel doesn't know about your model" (see below)

Can't we warn the user when creating the model, maybe similarly to the way "deleted" is treated here: https://github.com/neo4j-contrib/neomodel/blob/master/neomodel/core.py#L175?

Code:

class MyModel(StructuredNode):
    id = IntegerProperty()
    name = StringProperty()

node = MyModel(name="SomeName", id=2).save()
print(node)
node_get = MyModel.nodes.get(name="SomeName", id=2)
print(node_get)

Exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test/venv/lib/python3.8/site-packages/neomodel/match.py", line 524, in all
    return self.query_cls(self).build_ast()._execute(lazy)
  File "test/venv/lib/python3.8/site-packages/neomodel/match.py", line 499, in _execute
    results, _ = db.cypher_query(query, self._query_params, resolve_objects=True)            
  File "test/venv/lib/python3.8/site-packages/neomodel/util.py", line 36, in wrapper
    return func(self, *args, **kwargs)
  File "test/venv/lib/python3.8/site-packages/neomodel/util.py", line 259, in cypher_query
    results = self._object_resolution(results)
  File "test/venv/lib/python3.8/site-packages/neomodel/util.py", line 217, in _object_resolution
    raise NodeClassNotDefined(a_result_attribute[1], self._NODE_CLASS_REGISTRY)
neomodel.exceptions.NodeClassNotDefined: <exception str() failed>

(I am apparently not the only user who falls into this: https://github.com/neo4j-contrib/neomodel/issues/532)

PS, I am using last neomodel version:

Name: neomodel
Version: 4.0.8

stellasia avatar May 05 '22 07:05 stellasia

I've experienced the same with setting id as a UniqueIdProperty as so

class MyModel(StructuredNode):
    id = UniqueIdProperty()
    name = StringProperty()

in version 4.0.8.

ernestofgonzalez avatar Aug 03 '22 20:08 ernestofgonzalez

.id is ID() in cypher. https://neomodel.readthedocs.io/en/latest/getting_started.html#create-update-delete-operations

a-takahashi223 avatar Jan 12 '23 02:01 a-takahashi223

I am facing the same issue. I had an id property defined in my model

class Person(DjangoNode):
    id = properties.UniqueIdProperty()
    first_name = properties.StringProperty(unique_index=True, required=True, max_length=100)

And I have been trying to save new nodes from last 2 days

person = Person(first_name='John')
person.save()

Which is giving successful response but the node is not created in the database

<Person: {'id': 'da8d629ceb0c4d5ea0b36d691f644e25', 'first_name': 'John'}>

After debugging the applications source code, found that if hasattr(self, "id"): in the save() method is actually trying to update the existing record instead of creating based on the id field.

It should raise an error when the node does not exist with the given id instead of the success message or instead, it should raise an error if there is an id field defined in the model.

anuj9196 avatar Jun 24 '23 13:06 anuj9196

See release 5.1.2 #749

mariusconjeaud avatar Sep 22 '23 07:09 mariusconjeaud