BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.91k stars 201 forks source link

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

Closed amos402 closed 3 months ago

amos402 commented 4 months ago

Describe the bug Document.save failed to create a new doc if the _id field is a customized field. The save implementation changed in 9e89d30f0f22b2fa2b4fdfcea40e012402ba7dda 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.

roman-right commented 4 months ago

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

github-actions[bot] commented 3 months ago

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

github-actions[bot] commented 3 months ago

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