]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
SQLite strict tables
authorGuilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com>
Thu, 12 Dec 2024 20:11:27 +0000 (15:11 -0500)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 12 Dec 2024 20:57:45 +0000 (21:57 +0100)
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 [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..9a27ae9
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: usecase, sqlite
+    :ticket: 7398
+
+    Added SQLite table option to enable ``STRICT`` tables.
+    Pull request courtesy of Guilherme Crocetti.
index 0e4c9694bbffafc2a75b059ae9703927671b82a8..5ae7ffbf0f36db47c63e7d692c8787279bb89186 100644 (file)
@@ -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
     <https://www.sqlite.org/lang_createtable.html>`_
 
-
 .. _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}),
index 37a8da6abeb68d95f7a9d7e386eb47cb33b67176..246b9852329bb5530aa66adb2a7806589bad4076 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()