]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
ensure autocommit is not False for SQLite FK pragma
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 7 Aug 2025 14:48:51 +0000 (10:48 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 7 Aug 2025 14:51:57 +0000 (10:51 -0400)
References: #12767
References: https://github.com/python/cpython/issues/137205
Change-Id: I6772a4c9c216c6981421043f850cbf833ded2be3
(cherry picked from commit e96bf8608ed2862f538ac0873303c91ed3a7260c)

lib/sqlalchemy/dialects/sqlite/base.py

index cc43a826f5ac59a68f6989179e22d3f18b38c11a..5dbac009822d69bce65de549c9196f61dcb97fc1 100644 (file)
@@ -396,10 +396,18 @@ new connections through the usage of events::
 
     @event.listens_for(Engine, "connect")
     def set_sqlite_pragma(dbapi_connection, connection_record):
+        # the sqlite3 driver will not set PRAGMA foreign_keys
+        # if autocommit=False; set to True temporarily
+        ac = dbapi_connection.autocommit
+        dbapi_connection.autocommit = True
+
         cursor = dbapi_connection.cursor()
         cursor.execute("PRAGMA foreign_keys=ON")
         cursor.close()
 
+        # restore previous autocommit setting
+        dbapi_connection.autocommit = ac
+
 .. warning::
 
     When SQLite foreign keys are enabled, it is **not possible**