From e77273d0ba5c09d120c2582e94b96b781ebecb90 Mon Sep 17 00:00:00 2001 From: Guilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com> Date: Sat, 23 Nov 2024 14:57:06 -0300 Subject: [PATCH] New SQLite DDL feature for tables: STRICT Fixes: #7398 --- doc/build/changelog/unreleased_20/7398.rst | 6 ++++++ lib/sqlalchemy/dialects/sqlite/base.py | 15 +++++++++++++-- test/dialect/test_sqlite.py | 8 ++++++++ 3 files changed, 27 insertions(+), 2 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..1ec2c6fe16 --- /dev/null +++ b/doc/build/changelog/unreleased_20/7398.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: usecase, sqlite + :ticket: 7398 + + New SQLite DDL feature for tables: STRICT. + Pull request courtesy of Guilherme Crocetti. diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 84bb8937e1..3e8c0410d1 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -836,6 +836,13 @@ dialect in conjunction with the :class:`_schema.Table` construct: `SQLite CREATE TABLE options `_ +* ``STRICT``:: + + Table("some_table", metadata, ..., sqlite_strict=True) + + The ``sqlite_strict`` parameter is a boolean set to ``False`` by default. + +..versionadded:: 2.0.37 .. _sqlite_include_internal: @@ -1707,9 +1714,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): @@ -1944,6 +1954,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 736284bd29..d24a75f67d 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