Skip to content

[BUG] Beanie should have a way to handle an id field different than underlying _id #1111

@nahuel

Description

@nahuel

Seems like defining id on the beanie.Document base class is a miss-design, as it's very frequent that your domain model has an id field (business related, a very common field name) distinct from the underlying Mongo _id field.

For example:

import pydantic
import beanie

# Pure pydantic model, e.g. maybe generated from OpenAPI specs, used everywhere:
class User(pydantic.BaseModel):
    id : str
    name : str 
    # ...

# Now we want to store it as such on Mongo
class UserDB(beanie.Document, User)
   class Settings:
        collection_name = "users"

Pylance detects the bad override Base classes for class "UserDB" define variable "id" in incompatible way

This also happens when you must create Beanie models from existing Mongo collections that use a business related id field. In that case you cannot use Beanie.

As Pydantic doesn't support fields starting with _, maybe _id can be aliased to something more specific and less prone to collision, like mongoDocumentId and still be mapped to the underlying _id.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions