From: Federico Caselli Date: Tue, 28 Jul 2026 18:18:40 +0000 (+0200) Subject: Improve postgresql inherits docs X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=340a9467a0891beb00620a5f5043518f20c32987;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Improve postgresql inherits docs Clarify that inherited table columns are not copied by default and provide an example of a function that will copy them. Change-Id: I17811c1d3500fbb22a015226325004c1d7ade0c6 References: #13446 --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index e529f15b91..eb59d845a7 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1251,6 +1251,33 @@ identifier:: ), ) +SQLAlchemy does not automatically copy the columns from the inherited tables +mentioned in the ``postgresql_inherits`` argument into the new +:class:`_schema.Table`. To populate the new table columns reflection may be +used, or a function similar to the following one:: + + def get_parent_columns(tbl: sa.Table) -> list[sa.Column]: + return [ + sa.Column( + c.name, + c.type, + key=c.key, + nullable=c.nullable, + # Set system=true to omit from the CREATE TABLE statement + system=True, + ) + for c in tbl.columns + ] + + + documents = Table( + "some_table", + metadata, + *get_parent_columns(some_supertable), + # ... # add other columns here normally if needed + postgresql_inherits="some_supertable", + ) + ``ON COMMIT`` ^^^^^^^^^^^^^