Skip to content
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

SqlMagic.autocommit=True vs conn.autocommit = True #207

Open
jameshowison opened this issue Feb 3, 2022 · 1 comment
Open

SqlMagic.autocommit=True vs conn.autocommit = True #207

jameshowison opened this issue Feb 3, 2022 · 1 comment

Comments

@jameshowison
Copy link

Using ipython-sql 0.3.9 installed via conda together with postgres and I'm not sure if the SqlMagic.autocommit=True option is working.

This code

%load_ext sql
%config SqlMagic.autocommit=True
%sql postgresql://localhost/
%sql CREATE DATABASE my_test_db

Produces a InternalError: (psycopg2.errors.ActiveSqlTransaction) CREATE DATABASE cannot run inside a transaction block error.

Conversely, this works fine

import psycopg2 as pg

conn = pg.connect(host='localhost')
conn.autocommit = True
cur = conn.cursor()
cur.execute('CREATE DATABASE class_music_festival')
conn.close()

Anyone know if %config SqlMagic.autocommit=True is supposed to do the same as conn.autocommit = True

@edublancas
Copy link

I looked at the code to understand why this is failing.

It turns out, ipython-sql isn't using the psycopg2's autocommit feature. %config SqlMagic.autocommit=True sets a flag that causes ipython-sql to run COMMIT after each command; so, if we turn it on, running this:

CREATE DATABASE my_test_db

is the same as running this:

BEGIN; -- executed since psycopg2's autocommit is off
CREATE DATABASE my_test_db; -- executed by the user
COMMIT; -- executed by ipython-sql due to the autocommit=True

Which causes the error:

CREATE DATABASE cannot run inside a transaction block

@jameshowison my team maintains a fork of this project and we're tackling this issue already so feel free to share your feedback! ploomber#90

pmfischer pushed a commit to pmfischer/ipython-sql that referenced this issue Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants