-
Notifications
You must be signed in to change notification settings - Fork 320
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
create_database: CREATE DATABASE cannot run inside a transaction block #432
Comments
from sqlalchemy import create_engine
engine = create_engine('postgresql://user:hackme@localhost/postgres',
isolation_level="AUTOCOMMIT")
engine.execute("CREATE DATABASE testdb ENCODING 'utf8' TEMPLATE template1") So, perhaps we could engine for any postgresql driver with ...
elif url.drivername.startswith('postgresql'):
engine = sa.create_engine(url, isolation_level='AUTOCOMMIT')
... |
Hi guys, I have the same problem. How can I get this correction? |
It's strange, but I have the same problem but with dependency of the running environment. If I use sqlalchemy==1.3.13 then debug if PyCharm works correctly. Is there any solution? |
I have the same problem with debuging from PyCharm. Executing "CREATE DATABASE..." in context manager |
Another PyCharm user, same thing. I have no idea what about running tests in debug mode causes this to crop up... |
I still have this issue if I update to current SQLAlchemy |
any progress on this? |
Still getting this error myself. Works fine from command line and inside 'run', but breaks when using 'debug'. I've tried setting it to AUTOCOMMIT and it still fails. Reverted to SQLAlchemy==1.3.13 for the time being. |
Exact same thing as @ghaggart is describing.. have all the latest of the moment and still happening:
Suspition # 1: Suspition # 2: if engine.driver == 'psycopg2':
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
i1 = engine.raw_connection().isolation_level
engine.raw_connection().set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
i2 = engine.raw_connection().isolation_level
engine.raw_connection().set_isolation_level(1)
i3 = engine.raw_connection().isolation_level
breakpoint() results in:
If we set |
Same for me, I experience this issue only when trying to debug tests with Pycharm. As other said my problem disappears if the engine is created with the diff --git a/sqlalchemy_utils/functions/database.py b/sqlalchemy_utils/functions/database.py
index 9613e12..62531ff 100644
--- a/sqlalchemy_utils/functions/database.py
+++ b/sqlalchemy_utils/functions/database.py
@@ -531,7 +531,7 @@ def create_database(url, encoding='utf8', template=None):
if url.drivername == 'mssql+pyodbc':
engine = sa.create_engine(url, connect_args={'autocommit': True})
- elif url.drivername == 'postgresql+pg8000':
+ elif url.drivername in {'postgresql+pg8000', 'postgresql+psycopg2'}:
engine = sa.create_engine(url, isolation_level='AUTOCOMMIT')
else:
engine = sa.create_engine(url) Apart from understanding why this weird bug happens (its has probably to do with pydevd), couldn't we in the meanwhile apply this patch? Sqlalchemy's documentation states that this option can be used for the psycopg2 dialect (link). |
If someone face it trying to create test db with |
Recently after update from
sqlalchemy==1.3.13
(working fine) to1.3.14
(and upper) i discovered thatsqlalchemy_utils
module started to raiseCREATE DATABASE cannot run inside a transaction block
error when creating database.psycopg2-binary
version was same for both tests,2.8.5
.Reason is the following code in sqlalchemy_utils.functions.database, please see example to reproduce issue below, it works with 1.3.13 and does not work with 1.3.14:
It works if i acquire connection explicitly:
I suppose it is related to sqlalchemy/sqlalchemy#5182.
I suppose that acquiring connection and specifying explicitly connection level is better. What do you think?
The text was updated successfully, but these errors were encountered: