From e5839f6a71fb5b43c7b54606588b2274e6cbab52 Mon Sep 17 00:00:00 2001 From: KingOfKaste <47917339+KingOfKaste@users.noreply.github.com> Date: Thu, 20 Feb 2025 20:03:52 +0100 Subject: [PATCH] Fixes #12368 --- doc/build/changelog/unreleased_20/12368.rst | 7 +++++++ lib/sqlalchemy/dialects/sqlite/base.py | 18 ++++++++++++------ test/dialect/test_sqlite.py | 8 ++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 doc/build/changelog/unreleased_20/12368.rst diff --git a/doc/build/changelog/unreleased_20/12368.rst b/doc/build/changelog/unreleased_20/12368.rst new file mode 100644 index 0000000000..c8a8bee0b4 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12368.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, sqlite + :tickets: 12368 + + Fixed a bug that caused incorrect DDL when setting + :paramref:`.Table.sqlite_with_rowid` to ``False`` in combination with + :paramref:`.Table.sqlite_strict` to ``True``. diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index e0c0f6e809..96b2414cce 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -1758,12 +1758,18 @@ class SQLiteDDLCompiler(compiler.DDLCompiler): return text def post_create_table(self, table): - text = "" - if table.dialect_options["sqlite"]["with_rowid"] is False: - text += "\n WITHOUT ROWID" - if table.dialect_options["sqlite"]["strict"] is True: - text += "\n STRICT" - return text + table_options = [] + + if not table.dialect_options["sqlite"]["with_rowid"]: + table_options.append("WITHOUT ROWID") + + if table.dialect_options["sqlite"]["strict"]: + table_options.append("STRICT") + + if table_options: + return "\n " + ",\n ".join(table_options) + else: + return "" class SQLiteTypeCompiler(compiler.GenericTypeCompiler): diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 997ce89351..730bce1a14 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -1153,6 +1153,14 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE TABLE atable (id INTEGER) STRICT", ) + def test_create_table_without_rowid_strict(self): + m = MetaData() + table = Table("atable", m, Column("id", Integer), sqlite_with_rowid=False, sqlite_strict=True) + self.assert_compile( + schema.CreateTable(table), + "CREATE TABLE atable (id INTEGER) WITHOUT ROWID, STRICT", + ) + class OnConflictDDLTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = sqlite.dialect() -- 2.47.3