]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Update _NamingSchemaCallable to support Index
authorMartin Baláž <balaz@brightpick.ai>
Mon, 9 Mar 2026 17:19:04 +0000 (13:19 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 10 Mar 2026 21:06:26 +0000 (22:06 +0100)
<!-- Provide a general summary of your proposed changes in the Title field above -->

### Description
<!-- Describe your changes in detail -->
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
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

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: #<issue number>` 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: #<issue number>` 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)

lib/sqlalchemy/sql/schema.py
test/typing/plain_files/sql/schema.py

index db397195d978cd01f804bc680c4e6cac32600323..7de91565f6828f0826cfc9456800f890bebe9902 100644 (file)
@@ -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]
 
 
index 7e5f0547442866cf9370614dd0474c45a3e1d149..77fcf47dec18b686095062795d1b5513ff6c79be 100644 (file)
@@ -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,
+    }
+)