Siddhant Adhav
Siddhant Adhav

Reputation: 11

Beanie ODM interpreting id (key) as _id (ObjectID)

In my FastAPI application using Beanie ODM, I have a collection with a key named id which is a string, alongside the default MongoDB _id field. Due to certain constraints, I cannot rename the key in the database. When I insert a document with {'id': 'some string'}, it overwrites the _id field with 'some string', resulting in a validation error when retrieving the document.

class Demo(Document):
    id: Optional[str] = None  

    class Settings:
        name = 'some_collection'

I tried adding both _id and id fields but still the same behaviour

class Demo(Document):
    _id                     : Optional[ObjectId] = None
    id                      : Optional[str] = None  

    class Settings:
        name = 'some_collection'

Upvotes: 1

Views: 390

Answers (0)

Related Questions