From 0dbb9a081640f7f39c210800875eff192fb105cf Mon Sep 17 00:00:00 2001 From: Paul Moeller Date: Thu, 12 Mar 2026 16:44:19 +0000 Subject: [PATCH 1/5] fixes for SQLAlchemy 2.0 --- pykern/sql_db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pykern/sql_db.py b/pykern/sql_db.py index c66902af..55303d3a 100644 --- a/pykern/sql_db.py +++ b/pykern/sql_db.py @@ -298,8 +298,8 @@ def select_one_or_none(self, table_or_stmt, where=None): def __conn(self): if self._conn is None: self._conn = self.meta._engine.connect() - self._conn.execute("PRAGMA foreign_keys = ON;") self._txn = self._conn.begin() + self._conn.execute(sqlalchemy.text("PRAGMA foreign_keys = ON;")) return self._conn def __execute_table_or_stmt(self, method, table_or_stmt, where): @@ -374,7 +374,7 @@ def fixup_post_insert(self, db, values, inserted_primary_key): """ # we do not handle multi column inserted_primary_keys if self.has_primary_id and inserted_primary_key is not None: - values[self.primary_id] = inserted_primary_key[self.primary_id] + values[self.primary_id] = inserted_primary_key._mapping[self.primary_id] return values def fixup_pre_insert(self, session, values): From 5f9d777cd04d4cf6df3ba90da83fec3591ee2ff8 Mon Sep 17 00:00:00 2001 From: Paul Moeller Date: Thu, 12 Mar 2026 16:45:50 +0000 Subject: [PATCH 2/5] for #638 update SQLAlchemy version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a4901525..6357ec33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ dependencies = [ "setuptools>=66", "six>=1.9", "Sphinx>=1.3.5", - "SQLAlchemy>=1.4,<2", + "SQLAlchemy==2.0.48", "toml>=0.10", "tornado", "urllib3", From a53b0d6a474ac8aa686d95e971ca5eeee0ee5ef1 Mon Sep 17 00:00:00 2001 From: Paul Moeller Date: Thu, 12 Mar 2026 17:23:07 +0000 Subject: [PATCH 3/5] removed SQLAlchemy fixed version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6357ec33..0ae0f3f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ dependencies = [ "setuptools>=66", "six>=1.9", "Sphinx>=1.3.5", - "SQLAlchemy==2.0.48", + "SQLAlchemy", "toml>=0.10", "tornado", "urllib3", From ad7cefcd01867e53a0f1cf0e598d11e5d23c32db Mon Sep 17 00:00:00 2001 From: Rob Nagler <5495179+robnagler@users.noreply.github.com> Date: Tue, 28 Apr 2026 16:45:56 +0000 Subject: [PATCH 4/5] use autobegin=False to keep PRAGMA before transaction Restores the original pre-SA2 ordering (PRAGMA before begin) by disabling autobegin on the connection, rather than reordering the lines. Tested with SQLAlchemy 1.4 and 2.x. Co-Authored-By: Claude Sonnet 4.6 --- pykern/sql_db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pykern/sql_db.py b/pykern/sql_db.py index 55303d3a..0d5cc5a4 100644 --- a/pykern/sql_db.py +++ b/pykern/sql_db.py @@ -297,9 +297,9 @@ def select_one_or_none(self, table_or_stmt, where=None): def __conn(self): if self._conn is None: - self._conn = self.meta._engine.connect() - self._txn = self._conn.begin() + self._conn = self.meta._engine.connect().execution_options(autobegin=False) self._conn.execute(sqlalchemy.text("PRAGMA foreign_keys = ON;")) + self._txn = self._conn.begin() return self._conn def __execute_table_or_stmt(self, method, table_or_stmt, where): From 859fd7bac69e477147fd5800e65315930b874512 Mon Sep 17 00:00:00 2001 From: Rob Nagler <5495179+robnagler@users.noreply.github.com> Date: Tue, 28 Apr 2026 16:48:47 +0000 Subject: [PATCH 5/5] keep PRAGMA inside transaction (autobegin=False makes it safe) Co-Authored-By: Claude Sonnet 4.6 --- pykern/sql_db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pykern/sql_db.py b/pykern/sql_db.py index 0d5cc5a4..4c308776 100644 --- a/pykern/sql_db.py +++ b/pykern/sql_db.py @@ -298,8 +298,8 @@ def select_one_or_none(self, table_or_stmt, where=None): def __conn(self): if self._conn is None: self._conn = self.meta._engine.connect().execution_options(autobegin=False) - self._conn.execute(sqlalchemy.text("PRAGMA foreign_keys = ON;")) self._txn = self._conn.begin() + self._conn.execute(sqlalchemy.text("PRAGMA foreign_keys = ON;")) return self._conn def __execute_table_or_stmt(self, method, table_or_stmt, where):