Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem handling dots in DictField keys #2539

Open
MarSoft opened this issue Jul 15, 2021 · 5 comments
Open

Problem handling dots in DictField keys #2539

MarSoft opened this issue Jul 15, 2021 · 5 comments

Comments

@MarSoft
Copy link

MarSoft commented Jul 15, 2021

Consider the following code:

class TestModel(db.Document):
    test = db.DictField()

obj = TestModel()
obj.test['key.with.dot'] = 'abc'
obj.save()

I think this should work, because MongoDB allows dots in dict key names as of version 3.6.
But in fact I get the following error:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    obj.save()
  File "mongoengine/document.py", line 450, in save
    self._clear_changed_fields()
  File "mongoengine/base/document.py", line 547, in _clear_changed_fields
    field_name = data._reverse_db_field_map.get(part, part)
AttributeError: 'NoneType' object has no attribute '_reverse_db_field_map'

After some digging I found that _clear_changed_fields takes list of string keys from _get_changed_fields() where each key is a dot-delimited path into the object. Obviously this doesn't play well with keys containing dots.

I think it would be safer to store keys as tuples rather than dot-delimited strings, but it will probably involve too many changes?

Tested with MongoEngine==0.23.1 and python 3.9.5.

@kunal1gupta
Copy link

I think it's more of a python issue. We don't face the same issue when defining the field as an Embedded doc with its own class.
Python is not able to interpret dicts using dots, whereas Mongodb, which is closer to JS is.
If you are able to define the structure of the DictField as an EmbeddedDoc, might make it easier

@bagerard
Copy link
Collaborator

@MarSoft is right, MongoEngine tracks the fields that are being changed through _changed_fields using a "dot-delimited" convention to describe the path.

I'm not sure why the "dot-delimited" convention was chosen in the first place in _changed_fields, a tuple indeed sound like a better approach. I know it's useful for embedded fields and for list fields.

I don't think it would be such a big work to do the change but there might be edge cases that I'm not considering here

@princess-entrapta
Copy link

Is this issue still in 0.25.0 ? We use mongoengine in production and we have the same error 'Nonetype' object has no attribute '_reverse_db_field_map' but we're not sure at all where it could come from.

I do not reproduce OP bug by testing locally. In what conditions is this crash occurring ?

@rghotra
Copy link

rghotra commented Jun 5, 2023

Seems to only be an issue with object.save() - object.update(field=updatedField) works

@princess-entrapta
Copy link

We in the end had a problem with dots if field keys as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants