agronholm / agronholm/sqlacodegen

Add constraint naming convention configuration option

未关闭
#193 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement
主要语言
Python
星标
2.4k
派生
284
平均合并
2 天 1 小时
30 天内合并 PR
1

描述

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_

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。