﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27092	MIgrations: Creating and then renaming a ForeignKey fails when index and constraint created	Alexander Dutton	nobody	"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:

{{{#!python
    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."	Bug	new	Migrations	1.10	Normal				Unreviewed	0	0	0	0	0	0
