#23906: "ValueError: Found wrong number (0) of constraints" when migrating
backwards with from alter_unique_together migration.
-------------------------------------+-------------------------------------
     Reporter:  liavkoren            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Migrations           |                  Version:  1.7
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
  alter_unique_together migrations   |  Unreviewed
  constraints valueError             |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by ramiro):

 FYI,

 TL;DR: ''Review your schema as is exists before you start using Django
 migrations. Chances are that the order of fields in the unique_together
 constraint there doesn't match what the models.py says, and hasn't for a
 long time.''

 We found the same problem with a similar setup  (Django 1.6 + South
 migrations being ported to Django 1.7) and scenario.

 What happened in our case is that the history of the model involved is
 like this:

 1) Model got created without unique_together constraint
 2) A `unique_together = ('char_field', 'fk_field')` got added at some
 point. South migtation syntax:
 {{{
 db.create_unique(u'app_outgoingpaymentcategory', ['char_field',
 'fk_field_id'])
 }}}

 What happened is that stating from that point it seems South recorded
 fields of the unique_together in the wrong order because all the frozen
 representations of the model in every migration file created since then
 contained the following excerpt:

 {{{
 'Meta': {'unique_together': "(('fk_field', 'str_field'),)", 'object_name':
 'ModelName'},
 }}}

 So, when it came time to create a initial migration using Django 1.7
 native migration framework, it picked the original `('char_field',
 'fk_field')` read from the models.py file.

 But when:

 * No DB schema is created by Django 1.7 because it already exists and we
 let `migrate` fake the initial Django migrations (or we pass `--fake` to
 it) when migrating forward.
 * We try to migrate back to migration zero (i.e. the state where the
 South->Djnago 1.7 handover happened), Django detects the order of the
 constraint as it exists in the DB doesn't match what it should be (i.e.
 what models.py says)

 and then the error message reported by the OP is shown.

--
Ticket URL: <https://code.djangoproject.com/ticket/23906#comment:3>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.f84b55892ba7e2043622554ee1a7168f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to