Skip to content

[BUG] Bunnet update doesn't have the same behavior as beanie's update #32

@stefan-jiroveanu

Description

@stefan-jiroveanu

Describe the bug
So if I am trying to run update for sync operations, using bunnet I need to use pydantic by_alias inside model_dump, while for beanie I don't need to do that.

Under the hood, I think the async Beanie update path and the sync Bunnet update path actually behave differently when it comes to your field‐name ↔ Mongo‐alias mapping.

To Reproduce
This is my update method using bunnet update.

    def update(self, entity_id: Any, data: SyncT) -> SyncT:
        """
        Update an entity.

        Args:
            entity_id: The ID of the entity to update
            data: The updated entity data

        Returns:
            The updated entity

        Raises:
            ElementNotFoundException: If no entity with the given ID exists
        """
        # Ensure entity exists
        entity = self.get(entity_id)

        # Update data fields
        update_data = data.model_dump(exclude={"id"}, exclude_unset=True)
        updated_entity = entity.update({"$set": update_data})

        return updated_entity

This is my update method using beanie update

    async def update(self, entity_id: Any, data: AsyncT) -> AsyncT:
        """
        Update an entity.

        Args:
            entity_id: The ID of the entity to update
            data: The updated entity data

        Returns:
            The updated entity

        Raises:
            ElementNotFoundException: If no entity with the given ID exists
        """
        # Ensure entity exists
        entity = await self.get(entity_id)

        # Update data fields
        update_data = data.model_dump(exclude={"id"}, exclude_unset=True)
        updated_entity = await entity.update({"$set": update_data})

        return updated_entity

as you can see they are almost identical. Now I have an entity that has an is_active field, using beanie everything updates normally, but if I try to update the entity using bunnet is_active fields gets ignored unless I specify by_alias=True inside the model_dump.

Expected behavior
I expect both libraries' update method to behave the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions