warn on ambiguous naming convention case

Issue #4080 new
Mike Bayer repo owner created an issue

from https://bitbucket.org/zzzeek/alembic/issues/453, there are five possibilities when rendering the name of a constraint:

  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.

Case #3 is ambiguous.

propose a warning as:

diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py
index d93c916f6..1cf1e77ae 100644
--- a/lib/sqlalchemy/sql/naming.py
+++ b/lib/sqlalchemy/sql/naming.py
@@ -125,6 +125,12 @@ def _constraint_name_for_table(const, table):
         )
     elif isinstance(convention, _defer_none_name):
         return None
+    elif convention is not None and const.name is not None:
+        from sqlalchemy import util
+        util.warn(
+            "Ambiguous naming behavior for name '%s' on %r object; "
+            "naming convention not being applied but name does not make "
+            "use of f()" % (const.name, type(const)))


 @event.listens_for(Constraint, "after_parent_attach")

Comments (2)

  1. Log in to comment