Opened 10 years ago

Closed 6 years ago

#23121 closed Bug (fixed)

Infinte migrations when changing Meta options

Reported by: JockeTF Owned by: nobody
Component: Migrations Version: 1.7-rc-2
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Changing a model's meta options sometimes causes Django to generate an infinite number of migrations.

Create a model with meta options.

from django.db import models

class Pony(models.Model):
    date_created = models.DateTimeField()

    class Meta():
        ordering = ('-date_created',)

Make migrations.

$ python3 manage.py makemigrations
Migrations for 'infinite':
  0001_initial.py:
    - Create model Pony

Remove the meta options from the model.

from django.db import models

class Pony(models.Model):
    date_created = models.DateTimeField()

Make migrations.

$ python3 manage.py makemigrations
Migrations for 'infinite':
  0002_auto_20140728_1440.py:
    - Change Meta options on pony

$ python3 manage.py makemigrations
Migrations for 'infinite':
  0003_auto_20140728_1440.py:
    - Change Meta options on pony

$ python3 manage.py makemigrations
Migrations for 'infinite':
  0004_auto_20140728_1440.py:
    - Change Meta options on pony

I first encountered this issue when upgrading from Django 1.7b4 to 1.7c1. I do not know what exactly is causing this, but it happens in more cases than just when removing the meta options. In my project I have many migrations scripts for several different models. Two of the models ended up suffering from this issue after upgrading to Django 1.7c1. The issue is still present in Django 1.7c2. Squashing the migrations does not help. However, removing the migrations and starting anew solves the issue.

Below is an example of a squashed migration which suffers from this issue.

from django.db import models, migrations

class Migration(migrations.Migration):

    replaces = [('infinite', '0001_initial'), ('infinite', '0002_auto_20140728_1440')]

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Pony',
            fields=[
                ('id', models.AutoField(serialize=False, auto_created=True, primary_key=True, verbose_name='ID')),
                ('date_created', models.DateTimeField()),
            ],
            options={
                'ordering': ('-date_created',),
            },
            bases=(models.Model,),
        ),
        migrations.AlterModelOptions(
            name='pony',
            options={},
        ),
    ]

Starting from scratch solves the issue.

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Pony',
            fields=[
                ('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')),
                ('date_created', models.DateTimeField()),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]

Attachments (1)

migrations_bug.tar.gz (3.0 KB ) - added by bacilla 6 years ago.
test project

Download all attachments as: .zip

Change History (7)

comment:1 by Tim Graham, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

I can reproduce.

comment:2 by Andrew Godwin <andrew@…>, 10 years ago

Resolution: fixed
Status: newclosed

In d6e73a876d77e889efe839345d39690a7004e2f4:

Fixed #23121: AlterModelOptions operation not changing state right

comment:3 by Andrew Godwin <andrew@…>, 10 years ago

In 394053ce605a30c742d6270a80986b255adb3a30:

[1.7.x] Fixed #23121: AlterModelOptions operation not changing state right

comment:4 by JockeTF, 10 years ago

Thank you! I can confirm that this solves the issue.

comment:5 by bacilla, 6 years ago

Resolution: fixed
Status: closednew

The same bug in version 2.1. Test project in attachment.

Test model:

from django.db import models
from django.db.models.functions import Cast, Concat, Right


class TestEntity(models.Model):
    some_property = models.TextField()

    class Meta:
        ordering = ('some_property',)

Migrations log:

$ python manage.py makemigrations app
Migrations for 'app':
  app/migrations/0001_initial.py
    - Create model TestEntity

Then ordering have been changed.

class TestEntity(models.Model):
    some_property = models.TextField()

    class Meta:
        ordering = (
            Cast(models.Func(models.F('some_property'), models.Value('^\d+'), function='SUBSTRING'), models.IntegerField()),
            Right(Concat(models.Value('00000'), models.F('some_property')), models.Value(5))
        )

Migrations log:

$ python manage.py makemigrations app
Migrations for 'app':
  app/migrations/0002_auto_20180907_1712.py
    - Change Meta options on testentity
$ python manage.py makemigrations app
Migrations for 'app':
  app/migrations/0003_auto_20180907_1713.py
    - Change Meta options on testentity
$ python manage.py makemigrations app
Migrations for 'app':
  app/migrations/0004_auto_20180907_1713.py
    - Change Meta options on testentity
Last edited 6 years ago by bacilla (previous) (diff)

by bacilla, 6 years ago

Attachment: migrations_bug.tar.gz added

test project

comment:6 by Simon Charette, 6 years ago

Resolution: fixed
Status: newclosed

Please open a new ticket instead of re-opening this closed one.

The issue you're encountering is due to a bug in a new feature in 2.0 that allowed expressions to be used in Model.Meta.ordering (#26257) and has little to do with this Django 1.7 era bug.

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