weixin_39890652 2020-11-30 11:37
浏览 0

drop_constraint(... type_='check') working too well with naming conventions

Migrated issue, originally created by Matt Jernigan ()

v0.8.4

This:


op.drop_constraint('ck_forms_code_friendly', 'forms', type_='check')

tries to drop a constraint called ck_forms_ck_forms_code_friendly which is not consistent with the other drop constraint types that don't apply the naming convention here.

To drop ck_forms_code_friendly you must do it this way instead:


op.drop_constraint('code_friendly', 'forms', type_='check')

该提问来源于开源项目:sqlalchemy/alembic

  • 写回答

5条回答 默认 最新

  • weixin_39890652 2020-11-30 11:37
    关注

    Michael Bayer () wrote:

    hello -

    unfortunately there is a hard to explain intent behind this behavior and neither the documentation at http://alembic.zzzcomputing.com/en/latest/naming.html nor at http://docs.sqlalchemy.org/en/latest/core/constraints.html#constraint-naming-conventions attempt to explain it, and this should be fixed.

    The behavior of naming conventions is as follows:

    1. if the constraint has a name of None, then the naming convention is applied.

    2. if the constraint name has a name of None, and the naming convention includes the token %(constraint_name)s, then an error is raised: "Naming convention including %(constraint_name)s token requires that constraint is explicitly named."

    3. if the constraint has a non-None name, and the convention does not include the %(constraint_name)s token, then it is assumed that the constraint was given an explicit name, and the convention is not applied.

    4. if the constraint has a non-None name, and the convention includes the token %(constraint_name)s, then it is assumed that this name is part of fulfillment of the contract of the convention, and the naming convention is applied, using the existing name for %(constraint_name)s.

    5. if the constraint has an f() name, then no naming convention is applied ever.

    Alembic autogeneration will always be generate operations using the f() qualifier if they are based on a schema construct where a naming convention has already been applied. This was added in #183.

    Above, we can see out of the five possible options, the case named in 3 is the only one which is ambiguous and is giving you problems. I has nothing to do with CHECK constraints. I would favor use case #3 emitting a warning, e.g. always use f() or None for a constraint name when naming convention is in place, though the impact of this warning would need to be carefully tested.

    评论

报告相同问题?