agronholm / agronholm/sqlacodegen

Add constraint naming convention configuration option

Open
#193 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
2.4k
Forks
284
Avg merge
2d 1h
Merged PRs (30d)
1

Description

Moving the discussion here from #120

---

> One feature I think would be great to have is to allow customization of constraint naming conventions. We already have `utils.uses_default_name`, but it's currently not super useful because `sqlalchemy.MetaData.naming_convention` is always `{'ix': 'ix_%(column_0_label)s'}` by default. (Reference: https://docs.sqlalchemy.org/en/14/core/constraints.html#the-default-naming-convention)
>
> We can pass the naming conventions as command line args like
>
> ```shell
> sqlacodegen --conventions '"ix": "ix_%(column_0_label)s", "uq": "uq_%(table_name)s_%(column_0_name)s", "pk": "pk_%(table_name)s"'
> ```
>
> or maybe have it read from a JSON file for easier formatting.
>
> The implementation would be pretty straightforward. We just need to modify the MetaData in `cli.main`.
>
> We will also need to make `utils.uses_default_name` check `MetaData.naming_convention` properly; right now it just checks for abbreviations: `"fk", "pk", "ix", "ck", "uq"` but the actual Constraint classes (e.g., `PrimaryKeyConstraint`) are valid as well. (Reference: https://docs.sqlalchemy.org/en/14/core/constraints.html#configuring-a-naming-convention-for-a-metadata-collection)
>
> EDIT: formatting, typo

_Originally posted by @leonarduschen in https://github.com/agronholm/sqlacodegen/issues/120#issuecomment-1087870235_

---

> @leonarduschen sounds good on the general level. I do question naming conventions being passed on the command line. That rabbit hole goes pretty deep when everybody wants to customize every aspect of the code generation step. A YAML or TOML based configuration file would be better. I'm looking forward to seeing what you come up with.
>
> My intent with the next release is to create a system where you could have your own generator that just gets passed elements to be formatted (tables, classes, whatnot). If would then generate the code, optionally falling back to the default behavior.

_Originally posted by @agronholm in https://github.com/agronholm/sqlacodegen/issues/120#issuecomment-1092788085_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.