Home > Software design >  Cannot correct a failed migration django
Cannot correct a failed migration django

Time:02-04

I inadvertently did this:

ordering = models.IntegerField(default="Order/position")

I ran makemigrations and got no error. When I ran migrate it blew up with the error:

ValueError: invalid literal for int() with base 10: 'Order/position'

what I had meant to do was this:

ordering = models.IntegerField(default=0, verbose_name="Order/Position")

I updated to the correct field definition and while makemigrations is happy and noted the change migrate still keeps throwing that same error.

How do I fix this?

In case this matters - I am running Django with Postgres and both are in Docker containers

Is it possible to "cancel" a previously failed migration? Is it common for makemigration to not catch big errors like this?

CodePudding user response:

The best is probably to fix the migration file itself. In the migration file, it will probably looked like:

from django.db import migrations, models


class Migration(migrations.Migration):
    dependencies = [
        # …
    ]

    operations = [
        migrations.CreateModel(
            name='MyModel',
            fields=[
                (
                    'id',
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name='ID',
                    ),
                ),
                (
                    'ordering',
                    models.IntegerField(
                        default='Order/position',
                    ),
                ),
            ],
        )
    ]

You fix this by altering the definition for ordering to the IntegerField you described.

  • Related