﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31263	remote_field model caching issues in RenameModel migration.	Sean Esterkin	nobody	"I am encountering a bug when renaming a model with RenameModel. My project is fairly large with multiple applications and many migrations. With the project in its current state, the migrations run correctly. I have added a new migration with some earlier dependencies, which changed the order that some of the migrations ran in, and now one of the Many to Many tables foreign key column is not being renamed correctly.

I have tracked this issue down to the function `_populate_directed_relation_graph` within `django/db/models/options.py`. This function loops through all of the models and populates a related_objects_graph with each model pointing to a list of related fields.

Related fields are populated here
{{{
for f in fields_with_relations:
    if not isinstance(f.remote_field.model, str):
        related_objects_graph[f.remote_field.model._meta.concrete_model._meta].append(f)
}}}

and accessed here:
{{{
related_objects = related_objects_graph[model._meta.concrete_model._meta]
model._meta.__dict__['_relation_tree'] = related_objects
}}}

The bug seems to be that `f.remote_field.model._meta.concrete_model._meta` is not the same for every field pointing to what should be the same model. For example if my code looked like:

{{{
class M1(models.Model):
    field1 = models.BooleanField()

class M2(models.Model):
    m1 = models.ForeignKey(M1, on_delete=models.CASCADE)

class M3(models.Model):
   m1s = models.ManyToManyField(M1, related_name=""m3s"")
}}}

then the populated `related_objects` for M1 might look like
{{{
defaultdict(<class 'list'>, {<Options for M1>: [<django.db.models.fields.related.ForeignKey: m1>], <Options for M1>: [<django.db.models.fields.related.ManyToManyField: m1s>]}
}}}
where each <Options for M1> is slightly different.

This is very likely a caching bug. When I disable the @cached_property (see below) the bug does not occur. Additionally the bug only occurs when I run the migrations start to finish. If I run the migrations one at a time in the same order, this bug does not happen.


For diagnostics, I have found two changes to the Django code that cause this bug to stop occurring. Neither of these are good changes to the Django code.
The first is to change 
{{{
related_objects_graph[f.remote_field.model._meta.concrete_model._meta].append(f)
}}}
to 
{{{
related_objects_graph[f.remote_field.model._meta.concrete_model._meta.__str__()].append(f)
}}}
and
{{{
related_objects = related_objects_graph[model._meta.concrete_model._meta]
}}}
to
{{{
related_objects = related_objects_graph[model._meta.concrete_model._meta.__str__()]
}}}

The other fix involved editing the ` __get__` function on `cached_property` in `django/utils/functional.py`.
{{{
        if instance is None:
            return self
        res = instance.__dict__[self.name] = self.func(instance)
        return res
}}}
becomes
{{{
        if instance is None:
            return self
        return self.func(instance)
}}}
crippling the caching but fixing the caching bug."	Bug	closed	Migrations	2.2	Normal	needsinfo	migration, caching, remote_field, RenameModel		Unreviewed	0	0	0	0	0	0
