From 3b898ea61ec209ef3ef67e3a6bb55c69bae8c85d Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Tue, 26 Apr 2022 14:15:26 -0400 Subject: [PATCH] Fixed #29854 -- Made _all_related_fields() return deterministically ordered fields. Thanks to Rick Yang and Baptiste Mispelon for the investigation. --- django/db/backends/base/schema.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 2a37b138954..468ab424908 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -1,4 +1,5 @@ import logging +import operator from datetime import datetime from django.db.backends.ddl_references import ( @@ -35,11 +36,15 @@ def _is_relevant_relation(relation, altered_field): def _all_related_fields(model): - return model._meta._get_fields( - forward=False, - reverse=True, - include_hidden=True, - include_parents=False, + # Related fields must be returned in a deterministic order. + return sorted( + model._meta._get_fields( + forward=False, + reverse=True, + include_hidden=True, + include_parents=False, + ), + key=operator.attrgetter("name"), )