BeanieODM / beanie

Asynchronous Python ODM for MongoDB

Home Page:http://beanie-odm.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] `Document.save` failed if the `_id` field is a customized field

amos402 opened this issue · comments

Describe the bug
Document.save failed to create a new doc if the _id field is a customized field.
The save implementation changed in 9e89d30 cause it.

To Reproduce

class TestUser(Document):
    id: str
    name: str = Field(..., type=str, alias="_id")


async def test_model_insert():
    user = TestUser.model_construct()
    user.name = "test"
    await user.save()
pymongo.errors.OperationFailure: Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id', full error: {'ok': 0.0, 'errmsg': "Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id'", 'code': 66, 'codeName': 'ImmutableField'}

Expected behavior
The API should work as it said insert it if it does not yet exist.

Additional context
Add any other context about the problem here.

This is unexpected way to use models. Internally Beanie uses id as the _id field. I'll check if this can be adjusted to support your use case. For now please use id as the _id field

This issue is stale because it has been open 30 days with no activity.

This issue was closed because it has been stalled for 14 days with no activity.