#33185: sqlmigrate crashes given a RenameModel operation with a self-referencing
foreign key on MySQL
-----------------------------+------------------------------------
Reporter: Jacob Walls | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Comment (by Simon Charette):
I don't see an easy way to address this bug unfortunately.
The logic in `RenameModel.database_forwards`
[https://github.com/django/django/blob/514c16e85f7ac2512235f3b6413646627420e969/django/db/migrations/operations/models.py#L324-L347
that deals with related fields alterations] expects the table to be
effectively renamed by the time it calls `SchemaEditor.alter_field`. This
code was added by 4ce7a6bc84c68406e39f48550434faeef3277eba to fix #22750
and #22248.
We could add a hack of `schema_editor.collect_sql` somewhere but it seems
like the proper way of dealing with this issue would be to avoid dropping
constraints and recreating them which is wasteful and the reason why we
are doing table introspection against the old table name.
Maybe we should have a low level `SchemaEditor.rename_model` operation
that takes care of performing the constraint renames and implicit many-to-
many table renames by itself (like `rename_field` does). I could see
having `RenameConstraint` and `RenameIndex` operations with associated
`SchemaEditor.rename_(constraint|index)` method could also be useful in
the future.
----
In the end there's no way we can get `sqlmigrate` to be foolproof while
having the schema editor rely on introspection. The above solutions would
work for a migration with a single `[RenameModel]` operation but would
fail for a sequence of two due to the state drift between the database and
the model state caused by SQL capture of DDL statement. I think we should
either commit to not using introspection in schema editor and making
`sqlmigrate` a first class citizen that doesn't require a database
connection or 'wontfix' such tickets and possibly consider deprecating
this command in the future.
--
Ticket URL: <https://code.djangoproject.com/ticket/33185#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/073.146d503441cdddf546bd2cac5c2210db%40djangoproject.com.