From 330c0ed7c4cf915c1a9982d94bce01fe3c9e9f1e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 7 Apr 2020 01:02:55 -0400 Subject: [PATCH] Stopped rebuilding referenced tables multiple times on SQLite alterations. --- django/db/backends/sqlite3/schema.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index a4885c56324..d1b113f1b91 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -360,9 +360,15 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): self._remake_table(model, alter_field=(old_field, new_field)) # Rebuild tables with FKs pointing to this field if the PK type changed. if old_field.primary_key and new_field.primary_key and old_type != new_type: - for rel in new_field.model._meta.related_objects: - if not rel.many_to_many: - self._remake_table(rel.related_model) + related_models = set() + for remote_field in new_field.model._meta.related_objects: + # Ignore self-relationship since the table was already rebuilt. + if remote_field.related_model == model: + continue + if not remote_field.many_to_many: + related_models.add(remote_field.related_model) + for related_model in related_models: + self._remake_table(related_model) def _alter_many_to_many(self, model, old_field, new_field, strict): """Alter M2Ms to repoint their to= endpoints."""