{{{#!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.
* cc: bendavis78 (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:1>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:2>
* 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>
* stage: Unreviewed => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:4>
* 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>
* status: new => assigned
* owner: => andrewgodwin
--
Ticket URL: <https://code.djangoproject.com/ticket/22073#comment:6>
* 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>
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>