-
-
Notifications
You must be signed in to change notification settings - Fork 253
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
Rename Unique Constraint #246
Comments
Changes by nickretallack (@nickretallack):
|
Michael Bayer (@zzzeek) wrote:
relational databases dont generally have RENAME CONSTRAINT functions, so yes, drop and recreate.
if this is SQLite, it does not because SQLite is the only database that supports unnamed unique constraints. Issue https://bitbucket.org/zzzeek/sqlalchemy/issue/3244/sqlite-inspection-with will help in SQLAlchemy 1.0, which when done in concert with Alembic's new SQLite migrations feature, will make this somewhat possible, but since the constraint is not named, there's still a guessing game going on that might never be foolproof.
you can't drop constraints on SQLite (theres no ALTER TABLE..DROP CONSTRAINT), you can only recreate the whole table. If this isn't SQLite, then the constraint should have been given a name in which case it would have shown up with a DROP. If for some reason there's unnamed constraints on some other backend, then there's again no way to drop them, because "DROP CONSTRAINT" requires a name.
there are edge and exception cases that deal with Alembic having to navigate around this, implicitly created constraints and indexes and such, however if the DB is managing explicit constraints that the user doesn't want in the source code, that's again what include_object is for as far as autogenerate. |
Michael Bayer (@zzzeek) wrote: no bug here, no action to be taken, if you have further questions about how unique constraints work please bring them to the mailing list, thanks |
Changes by Michael Bayer (@zzzeek):
|
nickretallack (@nickretallack) wrote: This is in postgres. |
nickretallack (@nickretallack) wrote: The constraint isn't totally nameless. The database generates a name for it. |
Michael Bayer (@zzzeek) wrote: OK then you'd see it here. it would show up as a drop. |
Michael Bayer (@zzzeek) wrote: OK and why you dont see it is this: we reflect the uniques from PG, we see a UQ like "person_first_name_last_name_key". But what PG actually does is it also creates an INDEX with that name, e.g. there's a constraint and an index, we see both of these. Already we are confused as we have to tell the difference between these two. At this point the code goes into a super-cautious section where it only emits the DROP if guesses that these might be two unrelated objects. If I change that logic to be more liberal I get this:
which is wrong! because that index will be already dropped when that drop_constraint happens. this logic can be improved with additional information it will get in 1.0. But it's not changing anytime soon. It is much much much worse for autogenerate to create migrations that aren't there rather than miss some things, because the former issue happens on every single autogen run. so the logic is very conservative. |
Adrian (@thiefmaster) wrote:
At least PostgreSQL supports |
Migrated issue, originally created by nickretallack (@nickretallack)
I created a model with a UniqueConstraint like so:
Then I changed the constraint's name.
The migration it generated looked like this:
If I run this, I end up with two identical constraints in the database. Unfortunately, there doesn't seem to be a rename_constraint function anywhere. I suppose it can be handled by dropping the existing constraint and then recreating it? Alembic should detect and drop the existing constraint, right? But since it isn't named in the source code, I suppose Alembic can't find it? Either way, it could look at the existing constraints and realize that there isn't a corresponding one in the source code, and detect that the constraint must be renamed or dropped because of that. Unless you want to support people creating constraints on the database that don't exist in the code.
The text was updated successfully, but these errors were encountered: