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

fix: update pk field only raises unfriendly error #1873

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

waketzheng
Copy link
Contributor

Description

Close #1870

Motivation and Context

Explicit raises OptionalError when executing await model.save(update_fields=['id'])

How Has This Been Tested?

make ci

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link

codspeed-hq bot commented Feb 5, 2025

CodSpeed Performance Report

Merging #1873 will not alter performance

Comparing waketzheng:update-pk-field-only (981d835) with develop (4e47cff)

Summary

✅ 16 untouched benchmarks

@coveralls
Copy link

coveralls commented Feb 5, 2025

Pull Request Test Coverage Report for Build 13179624319

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 89.236%

Totals Coverage Status
Change from base Build 13160076672: 0.002%
Covered Lines: 6491
Relevant Lines: 7090

💛 - Coveralls

tests/test_model_methods.py Outdated Show resolved Hide resolved
tortoise/backends/base/executor.py Outdated Show resolved Hide resolved
@henadzit
Copy link
Contributor

@waketzheng I added benchmarks for updates, let's merge them first so we have a baseline for performance and can be sure that this changes doesn't impact it significantly.

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rebase on top of the latest changes in develop?

@@ -267,13 +267,15 @@ async def execute_update(
values = []
expressions = {}
for field in update_fields or self.model._meta.fields_db_projection.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current logic assumes that if no values and no expressions, then the user must have been passed pk as part of update_fields. That is a bit hard to follow. What do you think about the version below?

for field in update_fields or self.model._meta.fields_db_projection.keys():
  field_obj = self.model._meta.fields_map[field]
  if field_obj.pk:
    if update_fields:
      raise OperationalError(
                f"Can't update pk field, use `{self.model.__name__}.create` instead."
            )
    continue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should allow .update_fields=['id', 'xxx'] as the Django do. And show warning to make sure user want to do that.

tortoise/backends/base/executor.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug/Feature: Allow updating primary key of an object
3 participants