Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Error occurs when saving to a model with the keep_nulls = False setting #1048

Open
eac0de opened this issue Oct 10, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@eac0de
Copy link

eac0de commented Oct 10, 2024

Describe the bug
When I call save() from document with the keep_nulls = False setting:
pymongo.errors.OperationFailure: '$unset' is empty. You must specify a field like so: {$unset: {<field>: ...}}, full error: {'ok': 0.0, 'errmsg': "'$unset' is empty. You must specify a field like so: {$unset: {<field>: ...}}", 'code': 9, 'codeName': 'FailedToParse'}
This is happening because func get_top_level_nones return a empty dictionary when None-values is missing.
[{'$set': {'request_id': ...}}, {'$unset': {}}]

Expected result
The document with the keep_nulls = False setting to save successfully even if there are no None-values

My solution

in method save from Document in branch if self.get_settings().keep_nulls is False:

instead

        if self.get_settings().keep_nulls is False:
            return await self.update(
                SetOperator(
                    get_dict(
                        self,
                        to_db=True,
                        keep_nulls=self.get_settings().keep_nulls,
                    )
                ),
                Unset(get_top_level_nones(self)),
                session=session,
                ignore_revision=ignore_revision,
                upsert=True,
                **kwargs,
            )

do this

        if self.get_settings().keep_nulls is False:
            arguments  = [
                SetOperator(
                    get_dict(
                        self,
                        to_db=True,
                        keep_nulls=self.get_settings().keep_nulls,
                    )
                )
            ]
            nones = get_top_level_nones(self)
            if nones:
                arguments.append(
                    Unset(nones)
                )
            return await self.update(
                *arguments,
                session=session,
                ignore_revision=ignore_revision,
                upsert=True,
                **kwargs,
            )
Copy link
Contributor

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

@github-actions github-actions bot added the Stale label Nov 10, 2024
@staticxterm staticxterm added bug Something isn't working and removed Stale labels Nov 12, 2024
@staticxterm staticxterm changed the title Error occurs when saving to a model with the keep_nulls = False setting [BUG] Error occurs when saving to a model with the keep_nulls = False setting Nov 12, 2024
@eac0de
Copy link
Author

eac0de commented Nov 20, 2024

as a temporary solution - add a field with None to the model

class Exemple(Document):
      a: int
      for_beanie_err: None = Field(
        default=None,
        exclude=True,
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants