From 66f9f7d8094a309a70e17fc7027e43fbfeb2d2ec Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 13 Feb 2025 19:54:11 +0100 Subject: [PATCH] various improvements to the docs - add create table with partition examples in mysql Change-Id: Idc5c35519a0812f1d63be95c14afb9ce2b00ea93 (cherry picked from commit 9f11b63109cc4d1c5c0f268424fa83bb61460710) --- doc/build/orm/declarative_tables.rst | 2 +- lib/sqlalchemy/dialects/mysql/base.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/build/orm/declarative_tables.rst b/doc/build/orm/declarative_tables.rst index 2ec15e3bf5..a4b5cbfe66 100644 --- a/doc/build/orm/declarative_tables.rst +++ b/doc/build/orm/declarative_tables.rst @@ -423,7 +423,7 @@ allow mapping database types that can support multiple Python types, such as The above example maps the union of ``list[int]`` and ``list[str]`` to the Postgresql :class:`_postgresql.JSONB` datatype, while naming a union of ``float, -str, bool`` will match to the :class:`.JSON` datatype. An equivalent +str, bool`` will match to the :class:`_types.JSON` datatype. An equivalent union, stated in the :class:`_orm.Mapped` construct, will match into the corresponding entry in the type map. diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 1314ee3deb..4a52d1b67a 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -182,6 +182,31 @@ For fully atomic transactions as well as support for foreign key constraints, all participating ``CREATE TABLE`` statements must specify a transactional engine, which in the vast majority of cases is ``InnoDB``. +Partitioning can similarly be specified using similar options. +In the example below the create table will specify ``PARTITION_BY``, +``PARTITIONS``, ``SUBPARTITIONS`` and ``SUBPARTITION_BY``:: + + # can also use mariadb_* prefix + Table( + "testtable", + MetaData(), + Column("id", Integer(), primary_key=True, autoincrement=True), + Column("other_id", Integer(), primary_key=True, autoincrement=False), + mysql_partitions="2", + mysql_partition_by="KEY(other_id)", + mysql_subpartition_by="HASH(some_expr)", + mysql_subpartitions="2", + ) + +This will render: + +.. sourcecode:: sql + + CREATE TABLE testtable ( + id INTEGER NOT NULL AUTO_INCREMENT, + other_id INTEGER NOT NULL, + PRIMARY KEY (id, other_id) + )PARTITION BY KEY(other_id) PARTITIONS 2 SUBPARTITION BY HASH(some_expr) SUBPARTITIONS 2 Case Sensitivity and Table Reflection ------------------------------------- -- 2.39.5