Django 1.8.4 makemigrations CreateModel changes field's order
Asked Answered
X

0

0

We've just switched to Django 1.8.4 (from 1.6, so first time using migrations), and we've noticed an issue when using the makemigrations command. The issue happens when creating a new model that contains Foreign Keys. The command generates a migration file with the field order changed: it sets all the FKs last, and reorganizes them by alphabetical order.

Here's an example :

class AnotherRandomModel(models.Model):
    attr1 = models.FloatField()

class AnotherRandomModel2(models.Model):
    attr1 = models.FloatField()

class RandomModel(models.Model):
    fk2 = models.ForeignKey(AnotherRandomModel2)
    attr2 = models.FloatField()
    fk1 = models.ForeignKey(AnotherRandomModel)
    attr1 = models.FloatField()

That will generate this migration file :

class Migration(migrations.Migration):

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='AnotherRandomModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('attr1', models.FloatField()),
            ],
        ),
        migrations.CreateModel(
            name='AnotherRandomModel2',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('attr1', models.FloatField()),
            ],
        ),
        migrations.CreateModel(
            name='RandomModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('attr2', models.FloatField()),
                ('attr1', models.FloatField()),
                ('fk1', models.ForeignKey(to='inventorylab.AnotherRandomModel')),
                ('fk2', models.ForeignKey(to='inventorylab.AnotherRandomModel2')),
            ],
        ),
    ]

You can see how it kept the non-FK fields order, but set both FK at the end an re-ordered them.

That's quite disturbing not to have the same order on the model as on the database. Does anyone know how to force the command to keep the order of the model?

I know I can always edit manually the created migration file, but I'll like to avoid doing that.

Xanthate answered 8/9, 2015 at 9:44 Comment(6)
Why does it matter what order the fields are in here? Surely where order is important, in forms, admin etc you can specify the order.Sclerous
Well you can edit the migration file and change the order if you are really concerned about it.Penang
I know in pure Django it doesn't matter, but we do access and fill our database tables from many sources, usually with raw SQL, and not always with named parameters. We have our own internal standards, and all our 100+ other tables have the Foreign Keys first. I just don't understand why Django enforces his own order.Xanthate
@Penang Yep that's what I'll do, but I wanted to avoid having to change the migration file at every table creation, because I know that either me or someone in the team will one day forget to do so.Xanthate
It's a one off thing (setting up migrations for the first time) after that it wont matter.Penang
Nop, that will happen on every new model, and we do add new ones on a regular basis :/Xanthate

© 2022 - 2024 — McMap. All rights reserved.