[Django] #22073: Backwards migration on ManyToMany field does not remove intermediary table

12 views
Skip to first unread message

Django

unread,
Feb 16, 2014, 11:50:27 PM2/16/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+--------------------
Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
When a migration is performed where a ManyToMany field is added, the
intermediary table is automatically created. However, when performing a
backwards migration, this table is not deleted. Consider the following:


{{{#!python
# locations/models.py

from products.models import Product

class Location(models.Model):
name = models.CharField(max_length=100)
products_sold = models.ManyToManyField(Product)
}}}

We create our initial migration for locations and run it:
{{{
$ ./manage.py makemigrations locations
Migrations for 'locations':
0001_initial.py:
- Create model Location

$ ./manage.py migrate locations
Operations to perform:
Apply all migrations: locations
Running migrations:
Applying locations.0001_initial... OK
}}}

Now, if we want to, we should be able to undo that migration and re-apply
it. Often in development I re-do my initial migrations to keep things
simple before I do my first deployment. Unfortunately when you re-aplly
the migration we get an exception, because the backward migration didn't
remove the intermediary table:

{{{
$ ./manage.py migrate locations zero
Operations to perform:
Unapply all migrations: locations
Running migrations:
Unapplying locations.0001_initial... OK

$ ./manage.py migrate locations
Operations to perform:
Apply all migrations: locations
Running migrations:
Applying locations.0001_initial...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/tmp/djangotest/src/django/django/core/management/__init__.py",
line 427, in execute_from_command_line
utility.execute()
...
File "/tmp/djangotest/src/django/django/db/backends/utils.py", line 77,
in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/tmp/djangotest/src/django/django/db/backends/utils.py", line 61,
in execute
return self.cursor.execute(sql, params)
File "/tmp/djangotest/src/django/django/db/utils.py", line 93, in
__exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/tmp/djangotest/src/django/django/db/backends/utils.py", line 61,
in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation
"locations_location_products_sold" already exists
}}}

I've attached an example project where this can be reproduced.

--
Ticket URL: <https://code.djangoproject.com/ticket/22073>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 16, 2014, 11:51:43 PM2/16/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+--------------------------------------

Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by bendavis78):

* cc: bendavis78 (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:1>

Django

unread,
Feb 17, 2014, 4:38:00 PM2/17/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+--------------------------------------

Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by bendavis78):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:2>

Django

unread,
Feb 17, 2014, 5:57:13 PM2/17/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+--------------------------------------

Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by bendavis78):

* has_patch: 0 => 1
* needs_tests: 1 => 0


Comment:

I've created a pull request with a fix and a new test for the creation of
models with M2M fields: https://github.com/django/django/pull/2315

--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:3>

Django

unread,
Feb 18, 2014, 9:02:12 AM2/18/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
-------------------------------------+-------------------------------------

Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* stage: Unreviewed => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:4>

Django

unread,
Feb 22, 2014, 6:22:15 PM2/22/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+------------------------------------

Reporter: bendavis78 | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by MarkusH):

* stage: Ready for checkin => Accepted


Comment:

I think I found a bug in the patch:
https://github.com/django/django/pull/2315/files#r9973533

--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:5>

Django

unread,
Mar 7, 2014, 1:27:13 PM3/7/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+----------------------------------------
Reporter: bendavis78 | Owner: andrewgodwin
Type: Bug | Status: assigned
Component: Migrations | Version: master

Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------
Changes (by andrewgodwin):

* status: new => assigned
* owner: => andrewgodwin


--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:6>

Django

unread,
Mar 8, 2014, 6:44:58 PM3/8/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+----------------------------------------
Reporter: bendavis78 | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------
Changes (by Ben Davis <bendavis78@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"df2652c44836374bb8a691e755edbc153805f9cc"]:
{{{
#!CommitTicketReference repository=""
revision="df2652c44836374bb8a691e755edbc153805f9cc"
Fixed #22073 - Ensure CreateTable operation handles backwards migration
correctly when M2M fields are present
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:7>

Django

unread,
Mar 8, 2014, 6:44:59 PM3/8/14
to django-...@googlegroups.com
#22073: Backwards migration on ManyToMany field does not remove intermediary table
---------------------------------+----------------------------------------
Reporter: bendavis78 | Owner: andrewgodwin
Type: Bug | Status: closed
Component: Migrations | Version: master

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+----------------------------------------

Comment (by Andrew Godwin <andrew@…>):

In [changeset:"cdf6eba1816669bd77a28da903d931b5a4b15992"]:
{{{
#!CommitTicketReference repository=""
revision="cdf6eba1816669bd77a28da903d931b5a4b15992"
Merge pull request #2315 from bendavis78/issues/22073

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

--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:8>

Reply all
Reply to author
Forward
0 new messages