﻿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
37554	Model rename questions in makemigrations depend on PYTHONHASHSEED	Konstantinos Giannopoulos	Konstantinos Giannopoulos	"When several removed models have the same fields as an added model, `makemigrations` asks ""Was the model app.X renamed to app.Y?"" for each candidate in turn. The order of these questions is not stable: it depends on `PYTHONHASHSEED`, so the same code can ask about a different model first on every run. If the user answers ""y"", different runs produce different migrations.

Initial models (migrated):
{{{#!python
class EmailLog(models.Model):
    recipient = models.CharField(max_length=200)
    body = models.TextField()
    sent_at = models.DateTimeField()

class SmsLog(models.Model):
    recipient = models.CharField(max_length=200)
    body = models.TextField()
    sent_at = models.DateTimeField()

class PushLog(models.Model):
    recipient = models.CharField(max_length=200)
    body = models.TextField()
    sent_at = models.DateTimeField()
}}}

Replace them with one model with the same fields:
{{{#!python
class MessageLog(models.Model):
    recipient = models.CharField(max_length=200)
    body = models.TextField()
    sent_at = models.DateTimeField()
}}}

Running `makemigrations --dry-run` with different hash seeds and answering ""y"" to the first question:
{{{
PYTHONHASHSEED  first question about  migration
1               PushLog               Rename model PushLog to MessageLog; Delete model EmailLog; Delete model SmsLog
2               SmsLog                Rename model SmsLog to MessageLog; Delete model EmailLog; Delete model PushLog
5               EmailLog              Rename model EmailLog to MessageLog; Delete model PushLog; Delete model SmsLog
}}}

`MigrationAutodetector.generate_renamed_models()` iterates over `removed_models`, a `set` of `(app_label, model_name)` tuples, without sorting it, so the order follows string hashing. Most other loops in the autodetector iterate over `sorted(...)`.

Sorting the set makes the questions, and the resulting migration, deterministic:
{{{#!diff
-            for rem_app_label, rem_model_name in removed_models:
+            for rem_app_label, rem_model_name in sorted(removed_models):
}}}

Regression test for `tests/migrations/test_autodetector.py` (it uses six candidate models, so it fails on the current code for almost every hash seed):
{{{#!python
def test_rename_model_candidates_are_checked_in_sorted_order(self):
    """"""
    When several removed models match an added model, the rename questions
    are asked in a stable order, not in the order of a set (which depends on
    PYTHONHASHSEED).
    """"""
    asked = []

    class RecordingQuestioner(MigrationQuestioner):
        def ask_rename_model(self, old_model_state, new_model_state):
            asked.append(old_model_state.name)
            return False

    def log_model(name):
        return ModelState(
            ""app"",
            name,
            [
                (""id"", models.AutoField(primary_key=True)),
                (""recipient"", models.CharField(max_length=200)),
            ],
        )

    removed = [""SmsLog"", ""EmailLog"", ""PushLog"", ""FaxLog"", ""ChatLog"", ""MailLog""]
    self.get_changes(
        [log_model(name) for name in removed],
        [log_model(""MessageLog"")],
        RecordingQuestioner(),
    )
    self.assertEqual(asked, sorted(removed, key=str.lower))
}}}

A small project reproducing this is attached; its `ask_seeds.py` runs `makemigrations --dry-run` with eight different seeds and prints the results.
Happy to provide a fix myself.
"	Cleanup/optimization	assigned	Migrations	dev	Normal		autodetector, RenameModel, deterministic	Konstantinos Giannopoulos	Unreviewed	0	0	0	0	0	0
