-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default MetaData kwarg naming_convention can create issues with certain databases #4784
Comments
the workaround case looks like:
one problem to fix is that the above is convoluted because the internal "unnamed" constraint thing is leaking into the recipe, which is something nobody should have to know about. the logic at https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/sql/naming.py#L147 is involved with this, the _defer_none_name symbol is the issue here that it would be nicer if end-user code wasn't exposed to this. |
That worried me too. So it looks like Assuming the recipe stands as is... I think it would be better if there were a 'constant' value that could be imported for the recipe. Maybe something like
however... what if i make
then the alembic/etc docs would be...
The last thing I want to do is add a new feature/function to be supported, but this seems rather lightweight and more beneficial. I guess this is solving a bit of a fringe issue as (somehow) no one has identified this before - but more open source projects seem to be actively pushing Alembic to manage SqlAlchemy over the past year, so i'm thinking about the potential for issues with new users. |
what does "auto_constraint_name" do? as always, putting an opinion in here (e.g. it counts _12345 or something) means more bugs and requests to change it |
Right now, it would just be the code from your example above:
I can't stress my line above enough: The last thing I want to do is add a new feature/function to be supported, I'm trying to find a good balance between somehow exposing Another approach could also be leaving the naming convention stuff as-is, and catching the sqlite error for not having a constraint provided for (only) BOOL and ENUM during |
I don't want the string "autoname" to be in SQLAlchemy. I want the user to define that in the same place they define all their other naming components. it will be cut and pasted from the SQLAlchemy docs, but that is fine, they can see it, and they can change it and they can't come to me with some bizarre issue about a certain hardcoded name. no hardcoding, that is the goal. we just need a quick way to set up something that works like this:
exposing "unnamed" is just a bug. the logic that does the end-user function thing needs to be fixed.
that sounds hard |
ok. i'll try to implement this first!
I thought so too, but it popped in my head and there was a slight chance you'd say "oh there's a hook for that stuff already!" |
just remembered. a uuid4 isn't sufficient because the whole point of a naming convention is that it should repeat each time. so....the uuid either has to be based on something repeatable, which is hard to do, or we have to use column_0_name etc., which for CHECK constraint is very hard because a CHECK doesn't necessarily have any column objects in it, so perhaps a uuid based on "ck" + the constraint's order in the list of constraints of that particular type, not really sure. |
are there any other places where a fallback constraint could be invoked? i tried to find some but couldn't; the only spot I found was this initial column setup. if this is isolated, would it be acceptable to take the first 5 chars of |
do you mean, "constraint_name" but the name is not present? any constraint object can have "None" for the name and any naming convention can use "%(constraint_name)s".
when you have a CheckConstraint, it is usually created against a string SQL expression, so it has no column names. The name for the constraint must stay constant even if the SQL expression is changed since this is used to track alembic autogenerate. I think perhaps Boolean and Enum should maybe add additional state to the constraint so that a name can be generated, perhaps a link back to the datatype itself. |
column_0_name works fine for the CheckConstraint generated by these types:
|
generates:
|
but if you add this:
you get:
which also should be improved, there are no columns there so a nicer message should say that |
another option, the CheckConstraint created by Boolean and Enum is a so-called "type-bound" constraint, so perhaps they could derive from a different key in the naming convention, like "type_ck" that refers to column_0. |
e.g. there's a flag so we know the CheckConstraint is related to one of these datatypes. |
I've put the type-bound approach to the top of my task list. This seems the cleanest/simplest, as the issue I'm concerned about is "new users not understanding an implicit check constraint created for a default column type".
Having a default constraint name with uuid is a nice feature, but it looks like the type constraint approach would largely eliminate the need for it. All the other ways to create a constraint seem to be explicitly invoked and uniform across database back ends. Throwing an exception in those situations seems fine to me - the reason should be apparent and the developers probably want to name the constraint.
I'll handle Improving the error message you brought up in another ticket/pr.
|
of course. yes let's ad support for "type_ck". in 1.4 it can even default to something, since after all we have a default for indexes still in there. This would also eliminate the need for the "unnamed" symbol thing but I'm not sure we can change that part in 1.3, maybe we can but I have to remember how that works.
yes uuid I forgot that we need them to be deterministic which makes it less worth it.
yes let's make sure the exceptions are very descriptive b.c. right now I think they are just KeyError / TypeError crap, including that column_X_name thing which can easily be improved
great! |
The PR is shaping up like this:
|
OK, let me make one change, which is that all Constraint classes have a flag called "_type_bound" that we should use here, it's not worth a whole subclass just for this change, we can change the lookup as:
|
Thanks! This is actually great. After some tests on the subclass method, I couldn't get the same class to appear in 1.3 for ENUM because it defaults to using the lowercase |
@zzzeek sorry to pester you on this again. i've got some test cases running and using your approach as-is, not declaring a if this is not intentional - altering the if/else slightly, i can limit the yields generated by so instead of:
we see can see either:
Which do you prefer? |
that was what I had in mind....if people put an existing naming convention for "ck" why wouldn't that be used ?
if people are putting a naming convention in , that means they don't want DB-generated names at all, so they should try to get a name. falling back to "ck" maintains the current behavior. |
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct work on sqlalchemy#4790 regarding exception text * added some debug info to constraint naming exceptions; this is likely to change Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
jonathan vanasco has proposed a fix for this issue in the master branch: Fixes #4784 regarding constraints: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2224 |
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
* introduced new type_ck prefix for type-bound constraints; code and docs * misc unit tests to cover the type-bound items * added unit tests to ensure pep-compliant enum objects are used to generate a name as the docs instruct Fixes sqlalchemy#4790 regarding exception text: * added some debug info to constraint naming exceptions Change-Id: I48dd77301c10d6462847e2d00ac1412be20a74b2
The current suggested Metadata naming_convention dictionary runs into issues on migrating Boolean columns in SQLite databases as described here (sqlalchemy/sqlalchemy#3345) and here (sqlalchemy/sqlalchemy#4784). If for some reason the original ck value is necessary, it might be useful to have an additional note indicating this as an alternative. Thanks for considering, I love using this library!
Note: perhaps use the enum listener example in #5151 |
The alembic docs reference this naming convention: https://alembic.sqlalchemy.org/en/latest/naming.html
a Boolean/Enum column will try to create a constraint on sqlite by default, but
constraint_name
is not defined. this will cause an error inEngine.create_all(
Tasks:
[ ] have the docs use custom callables to offer an automatic constraint name.
[ ] long term: descope into another issue a fallback option for constraint name.
see referenced discussion for details of above.
The text was updated successfully, but these errors were encountered: