From: CaoRongkai <3123005531@mails.gdut.edu.cn> Date: Thu, 11 Jun 2026 21:19:16 +0000 (+0800) Subject: Document quoted_name use for PostgreSQL INHERITS (#13342) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ea1a30f4606eeaee7d696e0d1291a3501ff1fc8a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Document quoted_name use for PostgreSQL INHERITS (#13342) --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 936e7e63f0..e45b3c52e5 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1236,6 +1236,21 @@ constraints, enabling table inheritance hierarchies in PostgreSQL. Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...)) +For schema-qualified parent table names, use :class:`.quoted_name` with +``quote=False`` to prevent the dotted name from being quoted as a single +identifier:: + + from sqlalchemy.sql import quoted_name + + Table( + "some_table", + metadata, + ..., + postgresql_inherits=quoted_name( + "my_schema.some_supertable", quote=False + ), + ) + ``ON COMMIT`` ^^^^^^^^^^^^^ diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 1e04d8b254..c5c30c4f8f 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -5706,9 +5706,8 @@ class quoted_name(util.MemoizedSlots, str): :class:`_schema.Table`, :class:`_schema.Column`, and others. The class can also be passed explicitly as the name to any function that receives a name which - can be quoted. Such as to use the :meth:`_engine.Engine.has_table` - method with - an unconditionally quoted name:: + can be quoted, such as :meth:`.Inspector.has_table` with an + unconditionally quoted name:: from sqlalchemy import create_engine from sqlalchemy import inspect @@ -5721,6 +5720,11 @@ class quoted_name(util.MemoizedSlots, str): backend, passing the name exactly as ``"some_table"`` without converting to upper case. + A :class:`.quoted_name` object with ``quote=False`` may be passed to APIs + that apply automatic quoting in order to keep the given name unquoted, + such as when a PostgreSQL ``INHERITS`` option refers to a schema-qualified + table name like ``my_schema.some_table``. + """ __slots__ = "quote", "lower", "upper" diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index d6ab7c136c..867e1f3686 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -69,6 +69,7 @@ from sqlalchemy.orm import Session from sqlalchemy.sql import column from sqlalchemy.sql import literal_column from sqlalchemy.sql import operators +from sqlalchemy.sql import quoted_name from sqlalchemy.sql import table from sqlalchemy.sql import util as sql_util from sqlalchemy.sql.functions import GenericFunction @@ -531,6 +532,22 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): '( "Quote Me", "quote Me Too" )', ) + def test_create_table_inherits_schema_qualified_quoted_name(self): + m = MetaData() + tbl = Table( + "atable", + m, + Column("id", Integer), + postgresql_inherits=quoted_name( + "my_schema.parent_table", quote=False + ), + ) + self.assert_compile( + schema.CreateTable(tbl), + "CREATE TABLE atable (id INTEGER) " + "INHERITS ( my_schema.parent_table )", + ) + def test_create_table_partition_by_list(self): m = MetaData() tbl = Table(