From: Sean Anderson Date: Sun, 8 Nov 2020 00:52:04 +0000 (-0500) Subject: Support SQLite WITHOUT ROWID tables X-Git-Tag: rel_1_3_21~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=785cece910bb3546e02714b12ad0b75227c81a23;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support SQLite WITHOUT ROWID tables This adds support for creating tables WITHOUT ROWID in the SQLite dialect. WITHOUT ROWID tables were introduced in SQLite version 3.8.2 (2013-12-06). They do not use an implicit rowid column as the primary key. This may result in space and performance savings for tables without INTEGER primary keys and tables with composite primary keys. For more information about this feature, see the sqlite documentation [1]. [1] https://www.sqlite.org/withoutrowid.html Fixes: #5685 ### Checklist This pull request is: - [x] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. Closes: #5686 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5686 Pull-request-sha: 2b44782d1b3d858e31ce1ff8e08e197af37344d8 Change-Id: Ifcf727b0c07c90e267b79828a8e3fd7a8260a074 (cherry picked from commit 4b39b0f89dfd47dc9f5ba948e564c2afbbd44fef) --- diff --git a/doc/build/changelog/unreleased_13/5685.rst b/doc/build/changelog/unreleased_13/5685.rst new file mode 100644 index 0000000000..6f98cfa5de --- /dev/null +++ b/doc/build/changelog/unreleased_13/5685.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: sqlite, usecase + :tickets: 5685 + + Added ``sqlite_with_rowid=False`` dialect keyword to enable creating + tables as ``CREATE TABLE … WITHOUT ROWID``. Patch courtesy Sean Anderson. diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 4e9a81b1d4..cb60905106 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -584,6 +584,21 @@ or on a per-:class:`_engine.Engine` basis:: When using the per-:class:`_engine.Engine` execution option, note that **Core and ORM queries that use UNION may not function properly**. +SQLite-specific table options +----------------------------- + +One option for CREATE TABLE is supported directly by the SQLite +dialect in conjunction with the :class:`_schema.Table` construct: + +* ``WITHOUT ROWID``:: + + Table("some_table", metadata, ..., sqlite_with_rowid=False) + +.. seealso:: + + `SQLite CREATE TABLE options + `_ + """ # noqa import datetime @@ -1254,6 +1269,11 @@ class SQLiteDDLCompiler(compiler.DDLCompiler): return text + def post_create_table(self, table): + if table.dialect_options["sqlite"]["with_rowid"] is False: + return "\n WITHOUT ROWID" + return "" + class SQLiteTypeCompiler(compiler.GenericTypeCompiler): def visit_large_binary(self, type_, **kw): @@ -1461,7 +1481,13 @@ class SQLiteDialect(default.DefaultDialect): isolation_level = None construct_arguments = [ - (sa_schema.Table, {"autoincrement": False}), + ( + sa_schema.Table, + { + "autoincrement": False, + "with_rowid": True, + }, + ), (sa_schema.Index, {"where": None}), ( sa_schema.Column, diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 46ff6c9e7a..870f386bc0 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -1113,6 +1113,16 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL): "(q, p) IN (VALUES (?, ?), (?, ?))", ) + def test_create_table_without_rowid(self): + m = MetaData() + tbl = Table( + "atable", m, Column("id", Integer), sqlite_with_rowid=False + ) + self.assert_compile( + schema.CreateTable(tbl), + "CREATE TABLE atable (id INTEGER) WITHOUT ROWID", + ) + class OnConflictDDLTest(fixtures.TestBase, AssertsCompiledSQL):