aditya
aditya

Reputation: 133

OperationalError in Django Migrations

First I created a Model named 'userorders' having fields : id(by default), user_id, and order_id.

Then after realizing I don't need order_id, I deleted it from MySQL DB first (using Workbench), then I made changes in the model in Django, but it keeps showing errors now.

0013_orderdetails_userorders.py

migrations.CreateModel(
            name='userorders',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('user_id', models.IntegerField()),
                ('order_id', models.IntegerField()),
            ],
        ),

0014_remove_userorders_order_id.py

dependencies = [
        ('shoppingCart', '0013_orderdetails_userorders'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='userorders',
            name='order_id',
        ),
    ]

0015_userorders_order_id.py

dependencies = [
    ('shoppingCart', '0014_remove_userorders_order_id'),
]

operations = [
    migrations.AddField(
        model_name='userorders',
        name='order_id',
        field=models.IntegerField(default=None),
        preserve_default=False,
    ),

0016_remove_userorders_order_id.py

dependencies = [
        ('shoppingCart', '0015_userorders_order_id'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='userorders',
            name='order_id',
        ),
    ]

0017_auto_20200508_1639.py

dependencies = [
    ('shoppingCart', '0016_remove_userorders_order_id'),
]

operations = [
    migrations.RenameField(
        model_name='orderdetails',
        old_name='user_id',
        new_name='order_id',
    ),
]

ERRORS - when I try to make migrations for any changes I do (Changes are not reflected in DB)

MySQLdb._exceptions.OperationalError: (1091, "Can't DROP 'order_id'; check that column/key exists")
The above exception was the direct cause of the following exception:
django.db.utils.OperationalError: (1091, "Can't DROP 'order_id'; check that column/key exists")

Currently, userorders contains 2 fields- id and user_id

How can I fix this?

Upvotes: 0

Views: 382

Answers (1)

bb4L
bb4L

Reputation: 919

The problem is your workflow you should have removed the field, made the migration and applied the migration to your DB.

But since you now have the issue, you might be able to solve it with recreating the column order_id in the table (with workbench) and then do makemigrations & migrate then django can drop the column and shouldn't show the error.

Upvotes: 1

Related Questions