#26488: migrate raises InvalidBasesError if you rename a multitable inheritance
base model
----------------------------+------------------------------------
Reporter: chrisjrn | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
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 simonphilips):
It seems the above solution is broken as the bases should be updated after
renaming the model and before reloading any other models. Naively calling
super().state_forwards() doesn't work.
{{{
#!python
class RenameBaseModel(migrations.RenameModel):
def state_forwards(self, app_label, state):
apps = state.apps
model = apps.get_model(app_label, self.old_name)
model._meta.apps = apps
# Get all of the related objects we need to repoint
all_related_objects = (
f for f in model._meta.get_fields(include_hidden=True)
if f.auto_created and not f.concrete and (not f.hidden or
f.many_to_many)
)
# Rename the model
state.models[app_label, self.new_name_lower] =
state.models[app_label, self.old_name_lower]
state.models[app_label, self.new_name_lower].name = self.new_name
state.remove_model(app_label, self.old_name_lower)
# Repoint bases, this needs to be done before reloading any model
full_old_name = "%s.%s" % (app_label, self.old_name_lower)
full_new_name = "%s.%s" % (app_label, self.new_name_lower)
for state_model in state.models.values():
if full_old_name in state_model.bases:
state_model.bases = tuple(full_new_name if b ==
full_old_name else b
for b in state_model.bases)
# Repoint the FKs and M2Ms pointing to us
for related_object in all_related_objects:
if related_object.model is not model:
# The model being renamed does not participate in this
relation
# directly. Rather, a superclass does.
continue
# Use the new related key for self referential related
objects.
if related_object.related_model == model:
related_key = (app_label, self.new_name_lower)
else:
related_key = (
related_object.related_model._meta.app_label,
related_object.related_model._meta.model_name,
)
new_fields = []
for name, field in state.models[related_key].fields:
if name == related_object.field.name:
field = field.clone()
field.remote_field.model = "%s.%s" % (app_label,
self.new_name)
new_fields.append((name, field))
state.models[related_key].fields = new_fields
state.reload_model(*related_key)
state.reload_model(app_label, self.new_name_lower)
}}}
Note that the migration that initially creates SubA must be run before
this migration. If SubA is in a different app, that means you'll have to
update that app's migration and set its `run_before` attribute.
--
Ticket URL: <https://code.djangoproject.com/ticket/26488#comment:4>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.c8da706a32ed9eb6b484ccb1d1c930de%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.