From: Martin Baláž Date: Mon, 9 Mar 2026 17:19:04 +0000 (-0400) Subject: Update _NamingSchemaCallable to support Index X-Git-Tag: rel_2_0_49~17 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=d9667225cf11f4e04f79caf1301a6718b0468809;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Update _NamingSchemaCallable to support Index ### Description According to [the documentation](https://docs.sqlalchemy.org/en/21/core/metadata.html#sqlalchemy.schema.MetaData.params.naming_convention), the values associated with user-defined “token” keys in `naming_convention` should be callables of the form `fn(constraint, table)`, which accepts the constraint/index object and Table. However, the type alias `_NamingSchemaCallable` accepts only constraint in the first argument. I propose to update `_NamingSchemaCallable` to accept also an index. ### Example ```python import sqlalchemy def include_0_N_name( schema_item: sqlalchemy.Index | sqlalchemy.Constraint, table: sqlalchemy.Table, ) -> str: tokens = [] for column in schema_item.dialect_options.get('postgresql', {}).get('include', []): tokens.append("_") tokens.append(column.name) return "".join(tokens) metadata = sqlalchemy.MetaData( naming_convention={ "include_0_N_name": include_0_N_name, "ix": "%(table_name)s_%(column_0_N_name)s%(include_0_N_name)s_idx", "uq": "%(table_name)s_%(column_0_N_name)s%(include_0_N_name)s_key", "fk": "%(table_name)s_%(column_0_N_name)s%(include_0_N_name)s_fkey", "pk": "%(table_name)s_pkey", }, ) ``` ### Checklist This pull request is: - [x] A documentation / typographical / small typing error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] 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: #13161 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13161 Pull-request-sha: cf53bd722932741bceaba1dd16a52ba93ff579cc Change-Id: I0daf8a6eeb458aaa09f3392e00d98c27dbf8ca3c (cherry picked from commit 2513bc65721a7651adb711b075b31f81d4b10343) --- diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index db397195d9..7de91565f6 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -5357,7 +5357,10 @@ class Index( ) -_NamingSchemaCallable = Callable[[Constraint, Table], str] +_NamingSchemaCallable = Union[ + Callable[[Constraint, Table], str], + Callable[[Index, Table], str], +] _NamingSchemaDirective = Union[str, _NamingSchemaCallable] diff --git a/test/typing/plain_files/sql/schema.py b/test/typing/plain_files/sql/schema.py index 7e5f054744..77fcf47dec 100644 --- a/test/typing/plain_files/sql/schema.py +++ b/test/typing/plain_files/sql/schema.py @@ -66,3 +66,20 @@ NAMING_CONVENTIONS_STR = { } MetaData(naming_convention=NAMING_CONVENTIONS_STR) + + +def index_only(index: Index, table: Table) -> str: + return "index_only" + + +def constraint_only(constraint: Constraint, table: Table) -> str: + return "constraint_only" + + +# constraint-only callable or index-only callable +MetaData( + naming_convention={ + "ix": index_only, + "uq": constraint_only, + } +)