Opened 8 years ago

Last modified 8 years ago

#27092 closed Bug

MIgrations: Creating and then renaming a ForeignKey fails when index and constraint created — at Version 1

Reported by: Alexander Dutton Owned by: nobody
Component: Migrations Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alexander Dutton)

Minimal test case at https://github.com/ox-it/create-rename-foreignkey-testcase; example Travis run at https://travis-ci.org/ox-it/create-rename-foreignkey-testcase/jobs/153535976.

The following migration fails:

    operations = [
        migrations.CreateModel(
            name='Planet',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ],
        ),
        migrations.CreateModel(
            name='Star',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ],
        ),
        migrations.AddField(
            model_name='planet',
            name='parent_star',
            field=models.ForeignKey(to='crft.Star'),
        ),
        migrations.RenameField(
            model_name='planet',
            old_name='parent_star',
            new_name='star',
        ),
    ]

as it tries to create the foreign key constraint and index at the end against the parent_star_id field, which has already been renamed to star_id.

This arose for as when I wanted to replace a ForeignKey with another to a different model, but perform a data migration from the old to the new. The idea was: create new foreign key with temporary name, migrate data, remove old foreign key, rename the new one to the original name.

I can get round it by first renaming the old field to a temporary name and creating the new one with the name we want by the end, but this causes the backwards migration to fail for the same reason.

Change History (1)

comment:1 by Alexander Dutton, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top