odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

Engine not saving the model if it contains an Optional EmbeddedModel set to None

Open M4rk3tt0 opened this issue 2 years ago • 3 comments

Bug

I have an Optional EmbeddedModel inside my main model. If I don't set a value for it, when the engine tries to save the data in the database, I get the error: TypeError: 'NoneType' object is not iterable

Current Behavior

This is a basic example that shows this behavior (it's the same with the AIOEngine):

from odmantic import Model, EmbeddedModel, SyncEngine

class Profile(EmbeddedModel):
    name: str
    age: int

class User(Model):
    username: str
    password: str
    profile: Profile | None

engine = SyncEngine()

user = User(username="John", password="password")
engine.save(user)

Expected behavior

The engine should save the model even if the EmbeddedModel is initially not set.

Environment

  • ODMantic version: 0.9.0
  • MongoDB version: 6.0.1
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
             pydantic version: 1.10.2
            pydantic compiled: True
                 install path: D:\Projects\Python\odmantic-test\.venv\Lib\site-packages\pydantic
               python version: 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
                     platform: Windows-10-10.0.19043-SP0
     optional deps. installed: ['typing-extensions']

M4rk3tt0 avatar Oct 08 '22 11:10 M4rk3tt0

Hello, thanks for the bug report.

I'll fix this asap.

art049 avatar Oct 09 '22 17:10 art049

Hello. Thanks for the library.

Any predictions on when this will be merged?

paulovitorweb avatar Nov 28 '22 12:11 paulovitorweb

Hello, this is still an issue in odmantic 0.9.2

Creadeyh avatar Sep 15 '23 14:09 Creadeyh