Fixed #31064 -- Recreated auto-created many-to-many tables on primary key data type change on SQLite.

Both local and remote auto-created many-to-many relationships were
affected.
This commit is contained in:
Simon Charette 2020-04-17 00:39:24 -04:00 committed by Mariusz Felisiak
parent 2ba55b2905
commit a548280857
2 changed files with 38 additions and 1 deletions

View File

@ -361,12 +361,21 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# 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:
related_models = set()
for remote_field in new_field.model._meta.related_objects:
opts = new_field.model._meta
for remote_field in opts.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)
elif 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)
for related_model in related_models:
self._remake_table(related_model)

View File

@ -1275,6 +1275,16 @@ class OperationTests(OperationTestBase):
Tests the AlterField operation on primary keys changes any FKs pointing to it.
"""
project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
project_state = self.apply_operations('test_alflpkfk', project_state, [
migrations.CreateModel('Stable', fields=[
('ponies', models.ManyToManyField('Pony')),
]),
migrations.AddField(
'Pony',
'stables',
models.ManyToManyField('Stable'),
),
])
# Test the state alteration
operation = migrations.AlterField("Pony", "id", models.FloatField(primary_key=True))
new_state = project_state.clone()
@ -1294,8 +1304,26 @@ class OperationTests(OperationTestBase):
for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider")
if c.name == "pony_id"
][0]
m2m_fk_type, m2m_fk_null = [
(c.type_code, c.null_ok)
for c in connection.introspection.get_table_description(
cursor,
'test_alflpkfk_pony_stables',
) if c.name == 'pony_id'
][0]
remote_m2m_fk_type, remote_m2m_fk_null = [
(c.type_code, c.null_ok)
for c in connection.introspection.get_table_description(
cursor,
'test_alflpkfk_stable_ponies',
) if c.name == 'pony_id'
][0]
self.assertEqual(id_type, fk_type)
self.assertEqual(id_type, m2m_fk_type)
self.assertEqual(id_type, remote_m2m_fk_type)
self.assertEqual(id_null, fk_null)
self.assertEqual(id_null, m2m_fk_null)
self.assertEqual(id_null, remote_m2m_fk_null)
assertIdTypeEqualsFkType()
# Test the database alteration