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
Database transaction is not rollbacked #403
Comments
This isn't really related to FastAPI. So maybe you can rename the title. I could reproduce this that if a query is run before a transaction, the transaction isn't rolled back properly. https://github.com/encode/databases/blob/master/databases/core.py#L204 I guess rollback is called on the wrong connection, I'll take a look. Simple way to reproduce this: await database.execute(User.select())
async with database.transaction():
await database.execute(User.insert().values({}))
raise Exception() The insert will be persisted here. But if the |
@goteguru I think this is related to the parallel transactions #328 . Here https://github.com/encode/databases/blob/master/databases/core.py#L206 Any ideas? |
@aminalaee without parallel transactions a db abstraction layer is pretty much useless except for the most basic applications. I'm not sure trying to hide the connection object from the user is a good design choice (looks simple at first glance, but the hassle and confusion later nullify the benefits in the long term). At least the transaction context manager should expose a connection object. It's a bug nevertheless. The rolled back connection should be the same. Must check. |
I have not wrapped my head around the usage of contextvars in this scenario, but I just want to note that modifying the
The following transaction rollback test also passes:-
|
Got quite the same regression after 0.4.3 -> 0.5.5 upgrade. |
Looks like it was fixed in 0.6.0 🎉 |
When there are multiple queries executed in fastapi endpoint handler, the transaction rollback doesn't work properly.
Full working example is available at https://github.com/sedlar/fastapi-transaction-issue.
It happens when there are queries executed outside of transaction and inside one. It also happens when there are two independent transactions.
When the transaction explicitly acquires connection, everything works as expected.
This is regression against databases==0.2.6 and sqlalchemy==1.3.12
The text was updated successfully, but these errors were encountered: