Opened 10 years ago

Closed 10 years ago

#22035 closed Bug (fixed)

makemigrations when adding field with a unique_together constraint should create fields before constraints

Reported by: Chris Beaven Owned by: Anton Baklanov
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: antonbaklanov@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Added a slug field to a Tag model, and a unique_together constraint of [('site', 'slug')]

Running makemigrations resulted in a migration with the operations ordered like this:

    operations = [
        migrations.AlterUniqueTogether(
            name='tag',
            unique_together=set([(u'site', u'slug')]),
        ),
        migrations.AddField(
            model_name='tag',
            name='slug',
            field=models.SlugField(default=''),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='tag',
            name='name',
            field=models.CharField(max_length=20),
        ),
    ]

That choked when running the migration, but manually changing the constraint to happen after the field addition worked fine. Apart from the fact that I should probably have made this two migrations, would it be sensible to order the AlterUniqueTogether operations last?

Change History (3)

comment:1 by Anton Baklanov, 10 years ago

Cc: antonbaklanov@… added
Owner: set to Anton Baklanov
Status: newassigned
Triage Stage: UnreviewedAccepted

I can confirm this on current master.
Will try to write test+patch for this now.

comment:2 by Anton Baklanov, 10 years ago

Has patch: set
Version: 1.7-alpha-1master

comment:3 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 0bd92d68ade11b56124c7cdfbbfff6eb37899291:

Fixed #22035 -- reordered migration operations

Now AddField actions appear in operations list before AlterUniqueTogether
actions.

Thanks to SmileyChris for the report.

Note: See TracTickets for help on using tickets.
Back to Top