Fixed #22073 - Ensure CreateTable operation handles backwards migration correctly when M2M fields are present #2315

Merged
merged 1 commit into from Mar 8, 2014

2 participants

@MarkusH MarkusH commented on the diff Feb 22, 2014
django/db/backends/schema.py
@@ -268,6 +268,11 @@ def delete_model(self, model):
"""
Deletes a model from the database.
"""
+ # Handle auto-created intermediary models
+ for field in model._meta.local_many_to_many:
+ if field.rel.through._meta.auto_created:
+ self.delete_model(field.rel.through)
@MarkusH
Django member
MarkusH added a note Feb 22, 2014

What happens if you have two models with two through models like the following? I suspect an endless recursion. But haven't tried it.

class A(models.Model):
    b_s = models.ManyToManyField('B', through='AB')

class B(models.Model):
    a_s = models.ManyToManyField('A', through='BA')

class AB(models.Model):
    a = models.ForeignKey(A)
    b = models.ForeignKey(B)

class BA(models.Model):
    b = models.ForeignKey(B)
    a = models.ForeignKey(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrewgodwin andrewgodwin merged commit cdf6eba into django:master Mar 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment