#23091: Squashed migrations may try to add existing constraint if there was
RunPython between create and alter some-model migrations
-------------------------------+----------------------
     Reporter:  anonymous      |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Migrations     |    Version:  1.7-rc-1
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+----------------------
 I squashed a lot of migrations, some with RunPython functions. The
 squashed migration needed some fixing to work. At some point of the
 squashed migration there was a "CreateModel" operation that created some
 model with relation to User:


 {{{
 ('user', models.ForeignKey(blank=True, to_field='id',
 to=settings.AUTH_USER_MODEL, null=True)),
 }}}

 But as the models got edited there was a migration (rather pointless,
 caused by editable=False):


 {{{
 field=models.ForeignKey(blank=True, null=True, to_field='id',
 to=settings.AUTH_USER_MODEL, editable=False),
 }}}

 In between there was a RunPython which quite likely prevented squashing
 those two migrations into one. Running such migrations ended up with:

 {{{
  File "/var/www/buku/virtualenvs/1405079302/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 66, in execute
     return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: constraint
 "buku_vendor_offerredir_user_id_75e4400f46095260_fk_auth_user_id" for
 relation "buku_vendor_offerredirect" already exists

 }}}

 The SQL it was trying to run was:


 {{{
 ALTER TABLE "buku_vendor_offerredirect" ADD CONSTRAINT
 buku_vendor_offerredir_user_id_75e4400f46095260_fk_auth_user_id FOREIGN
 KEY ("user_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY
 DEFERRED []
 }}}

 It would be handy if that "self.cursor.execute(sql, params)" from
 django/db/backends/utils.py also printed the SQL it was trying to run so
 it would be easier to find the operation in migration (or make exception
 more verbose).

 I had like 4 such cases. Some quite serious like removing null=True and
 adding unique=True

-- 
Ticket URL: <https://code.djangoproject.com/ticket/23091>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.135420b6e1bb413d924a37b79fb447c5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to