Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: AttributeError: 'Connection' object has no attribute 'commit'

When using SQLAlchemy (version 1.4.44) to create, drop or otherwise modify tables, the updates don't appear to be committing. Attempting to solve this, I'm following the docs and using the commit() function. Here's a simple example

from sqlalchemy import create_engine, text

engine = create_engine("postgresql://user:password@connection_string:5432/database_name")
with engine.connect() as connection:
    sql = "create table test as (select count(1) as result from userquery);"
    result = connection.execute(text(sql))
    connection.commit()

This produces the error:

AttributeError: 'Connection' object has no attribute 'commit'

What am I missing?

like image 437
Brian Risk Avatar asked Apr 16 '26 17:04

Brian Risk


1 Answers

The comment on the question is correct you are looking at the 2.0 docs but all you need to do is set future=True when calling create_engine() to use the "commit as you go" functionality provided in 2.0.

SEE migration-core-connection-transaction

When using 2.0 style with the create_engine.future flag, “commit as you go” style may also be used, as the Connection features autobegin behavior, which takes place when a statement is first invoked in the absence of an explicit call to Connection.begin():

like image 98
Ian Wilson Avatar answered Apr 18 '26 07:04

Ian Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!