]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
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)
commitd9667225cf11f4e04f79caf1301a6718b0468809
tree834facc9c3fe4c0c9b219697acabcfa79acfb8cc
parent453f7ac6a3e4dea0904abefae6b33c31d44c4df3
Update _NamingSchemaCallable to support Index

<!-- 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