<!-- 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)
)
-_NamingSchemaCallable = Callable[[Constraint, Table], str]
+_NamingSchemaCallable = Union[
+ Callable[[Constraint, Table], str],
+ Callable[[Index, Table], str],
+]
_NamingSchemaDirective = Union[str, _NamingSchemaCallable]
}
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,
+ }
+)