1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Refs #30591 -- Adjusted table rebuild for non-pk relationship on SQLite.

The existing code was only accounting for primary key changes and not
all unique key fields that can be referenced.
This commit is contained in:
Simon Charette
2020-04-17 00:54:37 -04:00
committed by Mariusz Felisiak
parent a548280857
commit 5220ca8d8a
2 changed files with 25 additions and 11 deletions

View File

@@ -358,8 +358,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type))
# Alter by remaking table
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:
# Rebuild tables with FKs pointing to this field.
if new_field.unique and old_type != new_type:
related_models = set()
opts = new_field.model._meta
for remote_field in opts.related_objects:
@@ -367,15 +367,17 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if remote_field.related_model == model:
continue
if not remote_field.many_to_many:
related_models.add(remote_field.related_model)
elif remote_field.through._meta.auto_created:
if remote_field.field_name == new_field.name:
related_models.add(remote_field.related_model)
elif new_field.primary_key and remote_field.through._meta.auto_created:
related_models.add(remote_field.through)
for many_to_many in opts.many_to_many:
# Ignore self-relationship since the table was already rebuilt.
if many_to_many.related_model == model:
continue
if many_to_many.remote_field.through._meta.auto_created:
related_models.add(many_to_many.remote_field.through)
if new_field.primary_key:
for many_to_many in opts.many_to_many:
# Ignore self-relationship since the table was already rebuilt.
if many_to_many.related_model == model:
continue
if many_to_many.remote_field.through._meta.auto_created:
related_models.add(many_to_many.remote_field.through)
for related_model in related_models:
self._remake_table(related_model)