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 4 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 (4a1a39c) with develop (7cb24a5)

Summary

✅ 12 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

@@ -121,6 +123,8 @@ async def test_save_partial(self):
n_mdl = await self.cls.get(id=self.mdl.id)
self.assertEqual(n_mdl.name, "Test")
self.assertEqual(n_mdl.desc, "Something")
with pytest.raises(OperationalError):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to have a separate test case for this os it is documented with tests and it is not just a check in an existing test.

I would also suggest another test case:

o.save()
o.id = 123
with pytest.raises(OperationalError):
  o.save()

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 tried, but it's hard to do that -- because can't get the original id if it changed which make it difficult to know whether object only changed pk field value.

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