From 7daae15bc5c96f5b211a21f180b995c91e704eba Mon Sep 17 00:00:00 2001 From: Guilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:11:27 -0500 Subject: [PATCH] SQLite strict tables Added SQLite table option to enable ``STRICT`` tables. Fixes #7398 Closes: #12124 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12124 Pull-request-sha: e77273d0ba5c09d120c2582e94b96b781ebecb90 Change-Id: I0ffe9f6fc2c27627f53a1bc1808077e74617658a (cherry picked from commit 5b0eeaca61972cc75b7d50b11fbc582753518e61) --- doc/build/changelog/unreleased_20/7398.rst | 6 ++++++ lib/sqlalchemy/dialects/sqlite/base.py | 16 +++++++++++++--- test/dialect/test_sqlite.py | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 doc/build/changelog/unreleased_20/7398.rst diff --git a/doc/build/changelog/unreleased_20/7398.rst b/doc/build/changelog/unreleased_20/7398.rst new file mode 100644 index 0000000000..9a27ae99a7 --- /dev/null +++ b/doc/build/changelog/unreleased_20/7398.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: usecase, sqlite + :ticket: 7398 + + Added SQLite table option to enable ``STRICT`` tables. + Pull request courtesy of Guilherme Crocetti. diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 0e4c9694bb..5ae7ffbf0f 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -870,12 +870,18 @@ dialect in conjunction with the :class:`_schema.Table` construct: Table("some_table", metadata, ..., sqlite_with_rowid=False) +* + ``STRICT``:: + + Table("some_table", metadata, ..., sqlite_strict=True) + + .. versionadded:: 2.0.37 + .. seealso:: `SQLite CREATE TABLE options `_ - .. _sqlite_include_internal: Reflecting internal schema tables @@ -1754,9 +1760,12 @@ class SQLiteDDLCompiler(compiler.DDLCompiler): return text def post_create_table(self, table): + text = "" if table.dialect_options["sqlite"]["with_rowid"] is False: - return "\n WITHOUT ROWID" - return "" + text += "\n WITHOUT ROWID" + if table.dialect_options["sqlite"]["strict"] is True: + text += "\n STRICT" + return text class SQLiteTypeCompiler(compiler.GenericTypeCompiler): @@ -1991,6 +2000,7 @@ class SQLiteDialect(default.DefaultDialect): { "autoincrement": False, "with_rowid": True, + "strict": False, }, ), (sa_schema.Index, {"where": None}), diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 37a8da6abe..246b985232 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -1145,6 +1145,14 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE TABLE atable (id INTEGER) WITHOUT ROWID", ) + def test_create_table_strict(self): + m = MetaData() + table = Table("atable", m, Column("id", Integer), sqlite_strict=True) + self.assert_compile( + schema.CreateTable(table), + "CREATE TABLE atable (id INTEGER) STRICT", + ) + class OnConflictDDLTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = sqlite.dialect() -- 2.47.3