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

[BUG] AttributeError error in merge_models function after document update #1125

Open
HK-Mattew opened this issue Feb 17, 2025 · 0 comments · May be fixed by #1126
Open

[BUG] AttributeError error in merge_models function after document update #1125

HK-Mattew opened this issue Feb 17, 2025 · 0 comments · May be fixed by #1126
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@HK-Mattew
Copy link

Describe the bug
An error occurs in the merge_models function after defining a new field in the document update in the .update method that is not included in the pydantic model using the extra='allow' option.

Follow the code example below and the exception

To Reproduce

import asyncio

from beanie import Document, init_beanie
from pydantic import ConfigDict


async def main():
    class DocumentTestModelWithModelConfigExtraAllow(Document):
        model_config = ConfigDict(extra='allow')

    await init_beanie(
        connection_string='mongodb://localhost:27017/beanie',
        document_models=[DocumentTestModelWithModelConfigExtraAllow],
    )

    doc = DocumentTestModelWithModelConfigExtraAllow()
    await doc.insert()

    await doc.update({"$set": {"my_extra_field": 12345}})
    assert doc.my_extra_field == 12345


if __name__ == '__main__':
    asyncio.run(main())

Exception

Traceback (most recent call last):
  File "/home/myuser/Documents/.gits/myproject/test.py", line 402, in <module>
    asyncio.run(main())
  File "/home/myuser/.pyenv/versions/3.11.10/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/myuser/.pyenv/versions/3.11.10/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/myuser/.pyenv/versions/3.11.10/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/myuser/Documents/.gits/myproject/test.py", line 397, in main
    await doc.update({"$set": {"my_extra_field": 12345}})
  File "/home/myuser/.cache/pypoetry/virtualenvs/myproject-B1nOShX4-py3.11/lib/python3.11/site-packages/beanie/odm/actions.py", line 239, in wrapper
    result = await f(
             ^^^^^^^^
  File "/home/myuser/.cache/pypoetry/virtualenvs/myproject-B1nOShX4-py3.11/lib/python3.11/site-packages/beanie/odm/utils/state.py", line 85, in wrapper
    result = await f(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/myuser/.cache/pypoetry/virtualenvs/myproject-B1nOShX4-py3.11/lib/python3.11/site-packages/beanie/odm/documents.py", line 740, in update
    merge_models(self, result)
  File "/home/myuser/.cache/pypoetry/virtualenvs/myproject-B1nOShX4-py3.11/lib/python3.11/site-packages/beanie/odm/utils/parsing.py", line 27, in merge_models
    left_value = getattr(left, k)
                 ^^^^^^^^^^^^^^^^
  File "/home/myuser/.cache/pypoetry/virtualenvs/myproject-B1nOShX4-py3.11/lib/python3.11/site-packages/pydantic/main.py", line 856, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'DocumentTestModelWithModelConfigExtraAllow' object has no attribute 'my_extra_field'
@staticxterm staticxterm added bug Something isn't working good first issue Good for newcomers labels Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants