#33586: Cannot delete object (A) referenced by another object (B) if said object
(A) has a foreign key to a custom user.
-------------------------------+------------------------------------
Reporter: Jeremy Poulin | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Simon Charette):
* cc: Simon Charette (added)
Comment:
The following patch to the reproduction project demonstrates the core of
the issue
{{{#!diff
diff --git a/rmigrate/migrations/0002_create_sample_objects.py
b/rmigrate/migrations/0002_create_sample_objects.py
index 9a17299..efd6cbe 100644
--- a/rmigrate/migrations/0002_create_sample_objects.py
+++ b/rmigrate/migrations/0002_create_sample_objects.py
@@ -3,15 +3,21 @@
def up(apps, schema_editor):
A = apps.get_model('rmigrate', 'A')
+ B = apps.get_model('rmigrate', 'B')
User = apps.get_model('rmigrate', 'User')
-
+ assert A._meta.apps is apps
+ assert B._meta.apps is apps
+ assert User._meta.apps is apps
a = A(user=User.objects.first())
a.save()
def down(apps, schema_editor):
A = apps.get_model('rmigrate', 'A')
- print(A.objects.all())
-
+ B = apps.get_model('rmigrate', 'B')
+ User = apps.get_model('rmigrate', 'User')
+ assert A._meta.apps is apps
+ assert B._meta.apps is apps
+ assert User._meta.apps is apps
A.objects.first().delete()
print(A.objects.all())
}}}
The `apps` that are attached to the modesl is simply wrong when the
migration is applied backward. It seems the `.apps` cache invalidation
logic in `MigrationExecutor._migrate_all_backwards` and its subsequent
call doesn't account for a particular case that still needs to be
determined.
The following patch that invalidates the `apps` cache before unapplying
migrations demonstrates that
{{{#!diff
diff --git a/django/db/migrations/executor.py
b/django/db/migrations/executor.py
index eb738cf457..b0e0bef0d8 100644
--- a/django/db/migrations/executor.py
+++ b/django/db/migrations/executor.py
@@ -216,6 +216,7 @@ def _migrate_all_backwards(self, plan, full_plan,
fake):
self.progress_callback("render_success")
for migration, _ in plan:
+ states[migration].__dict__.pop('apps', None)
self.unapply_migration(states[migration], migration,
fake=fake)
applied_migrations.remove(migration)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33586#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107017fb4c4d87f-ce6b55b2-1c30-46f6-aec2-47a97eb8d63b-000000%40eu-central-1.amazonses.com.