Failing to migrate a foreign key from one model to another

I have three models: assembly, configurator, and genus. I want to move a "genus" foreign key from the assembly model to the configurator model. I moved it in the python code, generated a migration, and but that fails utterly:

django.db.utils.OperationalError: (1025, "Error on rename of './isotek_intranet_django/aion_assembly' to './isotek_intranet_django/#sql2-848-19e5' (errno: 152)")

Which is like useful but not. My backend database is MariaDB. Here is the relevant bits of the migration:

operations = [
    migrations.RemoveField(
        model_name='assembly',
        name='genus',
    ),
    migrations.AddField(
        model_name='configurator',
        name='genus',
        field=models.ForeignKey(default=1, to='aion.Genus'),
    ),
]

What (and why) am I doing wrong?

Note that I can run my tests just fine. It is updating a currently existing database that fails.

Django is 1.8 release.

Upvotes: 0

Views: 1090

Answers (2)

dauut
dauut

Reputation: 21

Not exactly this error no but, I was having a very similar error:

django.db.utils.OperationalError: (1025, 'Error on rename of ... (errno: 150 Foreign key constraint is incorrectly formed

Dropping foreign key didn't solve my problem. However, I figured out that is a charset error. So, if you have this error probably you are trying to create new table with a different charset. So, setting default charset collation across the database solved my issue. In my case, I had error on django unit test db so;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env('DB_NAME'),
        'USER': env('DB_USERNAME'),
        'PASSWORD': env('DB_PASSWORD'),
        'HOST': env('DB_HOST'),
        'PORT': '',
        'TEST': {
            'CHARSET': 'latin1',
            'COLLATION': 'latin1_swedish_ci',
        }
    },

}

this configuration worked.

Upvotes: 0

gbs
gbs

Reputation: 1325

You're possibly running into this bug. In this case it means that you'll have to delete the constraint before dropping the foreign key. As a short term fix you can try to do this manually by following this answer.

Upvotes: 1

Related Questions