diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 70fa99404..403a2cf69 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -23,6 +23,7 @@ Fixed ^^^^^ - Fix `DatetimeField` use '__year' report `'int' object has no attribute 'utcoffset'`. (#1575) - Fix `bulk_update` when using custom fields. (#1564) +- Fix `get_or_create` method. (#1404) 0.20.1 ------ diff --git a/tortoise/models.py b/tortoise/models.py index bf2ccedd1..aebdf5b19 100644 --- a/tortoise/models.py +++ b/tortoise/models.py @@ -32,7 +32,6 @@ IntegrityError, OperationalError, ParamsError, - TransactionManagementError, ) from tortoise.fields.base import Field from tortoise.fields.data import IntField @@ -1068,7 +1067,6 @@ async def get_or_create( :param using_db: Specific DB connection to use instead of default bound :param kwargs: Query parameters. :raises IntegrityError: If create failed - :raises TransactionManagementError: If transaction error """ if not defaults: defaults = {} @@ -1077,13 +1075,13 @@ async def get_or_create( return await cls.filter(**kwargs).using_db(db).get(), False except DoesNotExist: try: - async with in_transaction(connection_name=db.connection_name) as connection: - return await cls.create(using_db=connection, **defaults, **kwargs), True - except (IntegrityError, TransactionManagementError) as exc: + return await cls.create(using_db=db, **defaults, **kwargs), True + except IntegrityError as exc: try: return await cls.filter(**kwargs).using_db(db).get(), False - except Exception: - raise exc + except DoesNotExist: + pass + raise exc @classmethod def select_for_update(