odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

Problem with list of EmbeddedModel whose fields use key_name

Open iXB3 opened this issue 3 years ago • 0 comments

I found problems with list of embedded model whose fields use key_name: the embedded list of document saved in db does not use key_name (following a code to reproduce the issue)

import odmantic as odm
import typing as tp
 
class ListEl(odm.EmbeddedModel):
    a: int = odm.Field(key_name='in-a')

class Out(odm.Model):
    o: tp.Optional[str] = odm.Field(key_name='out-o')
    inner: tp.List[ListEl] = odm.Field(key_name='in')

out = Out(inner=[ListEl(a=3), ListEl(a=4)], o='ciao')
engine = odm.SyncEngine()
engine.save(out)
# document saved: {"_id" : ObjectId(...), "in" : [{"a" : 3},{"a" : 4}],"out-o" : "ciao"}

The document should be {"_id" : ObjectId(...), "in" : [{"in-a" : 3},{"in-a" : 4}],"out-o" : "ciao"}

Probably once resolved the insertion, there will be a problem similar to #260 in resuming document from db (I'm testing my code on an already populated db and the problem arises).

Environment

  • ODMantic version: 0.8.0
  • MongoDB version: 5.0.3
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())): pydantic version: 1.10.1 pydantic compiled: True install path: C:\Users\utente\Ploovium\Repos\Ploovium-UtilsNEW\venv\Lib\site-packages\pydantic python version: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] platform: Windows-10-10.0.19043-SP0 optional deps. installed: ['typing-extensions']

iXB3 avatar Sep 09 '22 08:09 iXB3