#27092: MIgrations: Creating and then renaming a ForeignKey fails when index and
constraint created
-----------------------------+--------------------------------------
     Reporter:  alexsdutton  |                    Owner:  nobody
         Type:  Bug          |                   Status:  new
    Component:  Migrations   |                  Version:  1.10
     Severity:  Normal       |               Resolution:
     Keywords:               |             Triage Stage:  Unreviewed
    Has patch:  0            |      Needs documentation:  0
  Needs tests:  0            |  Patch needs improvement:  0
Easy pickings:  0            |                    UI/UX:  0
-----------------------------+--------------------------------------
Changes (by alexsdutton):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> 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.

New description:

 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27092#comment:1>
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/069.05b60e9c3752e0abe4e0d0964b5e6f11%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to