Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

IndexError on force_rollback of Transaction in tests. #570

@EdgyNortal

Description

@EdgyNortal

MRE:

import databases

@pytest.fixture(scope="session")
def db():
    return  databases.Database("postgres://...")

@pytest.fixture()
async def transaction(db):
    await db.connect()
    async with db.transaction(force_rollback=True):
        yield db


async def test_example(transaction):
    await transaction.execute("select 1")
___________________________________ ERROR at teardown of test_example ___________________________________

db = <databases.core.Database object at 0x7f121c90ac80>

    @pytest.fixture()
    async def transaction(db):
        await db.connect()
>       async with db.transaction(force_rollback=True):

tests/query/test_core.py:306: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv/lib/python3.10/site-packages/databases/core.py:435: in __aexit__
    await self.rollback()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <databases.core.Transaction object at 0x7f121ca7d210>

    async def rollback(self) -> None:
        print("rollback called")
        async with self._connection._transaction_lock:
>           assert self._connection._transaction_stack[-1] is self
E           IndexError: list index out of range

.venv/lib/python3.10/site-packages/databases/core.py:482: IndexError

I have included prints in the __aenter__ and __aexit__ of the Transaction class.

__aenter__
Connection: <databases.core.Connection object at 0x7f121ca7d0c0>
TransactionStack: [<databases.core.Transaction object at 0x7f121ca7d210>]

--------------------------------------- Captured stdout teardown ----------------------------------------
__aexit__
Connection: <databases.core.Connection object at 0x7f121c4496c0>
TransactionStack: []

Somehow the transaction connection on entry != the connection on exit.

Can someone point out what I am doing wrong, or how this could be possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions