]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
New SQLite DDL feature for tables: STRICT 12124/head
authorGuilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com>
Sat, 23 Nov 2024 17:57:06 +0000 (14:57 -0300)
committerGuilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com>
Sat, 23 Nov 2024 20:36:55 +0000 (17:36 -0300)
Fixes: #7398
doc/build/changelog/unreleased_20/7398.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

diff --git a/doc/build/changelog/unreleased_20/7398.rst b/doc/build/changelog/unreleased_20/7398.rst
new file mode 100644 (file)
index 0000000..1ec2c6f
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: usecase, sqlite
+    :ticket: 7398
+
+    New SQLite DDL feature for tables: STRICT.
+    Pull request courtesy of Guilherme Crocetti.
index 84bb8937e16f5e08480fcca4ec7efca90d862355..3e8c0410d15947eccb94e5e08924b74d59d66fb1 100644 (file)
@@ -836,6 +836,13 @@ dialect in conjunction with the :class:`_schema.Table` construct:
     `SQLite CREATE TABLE options
     <https://www.sqlite.org/lang_createtable.html>`_
 
+* ``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}),
index 736284bd29428e6b8e87ab1a7f24a2b85aadb0bd..d24a75f67d6a394106648169a9fb0b98f1ff7b1a 100644 (file)
@@ -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()