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
Fixed get_or_create method #1404
Conversation
@@ -77,11 +77,3 @@ async def test_nonconcurrent_get_or_create(self): | |||
self.assertEqual(len(una_created), 1) | |||
for una in unas: | |||
self.assertEqual(una[0], unas[0][0]) | |||
|
|||
@test.skipIf(sys.version_info < (3, 7), "aiocontextvars backport not handling this well") | |||
async def test_concurrent_get_or_create(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this testcase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it fails in the TestConcurrencyTransactioned
. But the same test works in the TestConcurrencyIsolated
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase on actual develop and add changes to changelog
tortoise/models.py
Outdated
except (IntegrityError, TransactionManagementError) as exc: | ||
try: | ||
return await cls.filter(**kwargs).using_db(db).get(), False | ||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this try-catch seems like debug artefact? Remove it please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to re-raise integrity exception instead of DoesNotExist error
) | ||
except DoesNotExist: | ||
try: | ||
async with in_transaction(connection_name=db.connection_name) as connection: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need transaction here for just single create?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not. But this logic is in django.
https://github.com/django/django/blob/main/django/db/models/query.py#L938
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this sub-transaction is used, so that in case of integrity error here - it aborts it's transaction, so we should have one here for it to be aborted, otherwise it can abort whole parent transaction that is out of scope of this function, which would lead for unexpected behaviour for user
Let's make similar transaction here, as it was in your previous version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
42e90ce
to
982d95b
Compare
Please rebase and resolve conflict |
ba2ed92
to
63cfb50
Compare
Pull Request Test Coverage Report for Build 9043068735Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Description
I encountered an error: with simultaneous identical requests, the server responds with 500 error code.
tortoise-orm 0.19.3
How Has This Been Tested?
After applying this PR, everything works as expected:
I did't find a way to reproduce this error in autotests.
Checklist:
My change requires a change to the documentation.I have updated the documentation accordingly.I have added tests to cover my changes.