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 5efd0d9c99..aead991479 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -583,6 +583,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 @@ -1259,6 +1274,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): @@ -1466,7 +1486,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 d06cd48f5f..57529aeb6d 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -1161,6 +1161,16 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL): ), ) + 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):