elethan elethan - 1 month ago 22x
MySQL Question

Django table not created when running migrations that explicitly create table.

While experimenting with some major changes to my Django models I seriously messed everything up. Luckily, I had not committed any changes, so I dropped all the changes. The next step was to get my database back the way it was before. Eventually, I got to the point where I could successfully run the

commands without issue. However, whenever I try to visit the site, I get the
saying that my table
does not exist.

I have the following migration file in my

# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-08-25 15:52
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

dependencies = [
('topspots', '0018_siteuser_share_location'),

operations = [
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('message', models.TextField()),
('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='recipient_notification', to=settings.AUTH_USER_MODEL)),
('sender', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='sender_notification', to=settings.AUTH_USER_MODEL)),

Which is supposed to create that table. If I try to run this migration specifically, it will fail while trying to unapply later migrations that reference that table, since that table does not exist. I have tried deleting the above migration (where my
table is created), and every model that comes after it. Then I run
again, but it says there are no migrations to apply, and my table has still not been created.

My question is: why isn't my table getting created when I run migrations? I know that I could manually create the table in MySQL, but I would like to know what I did wrong to mess up my database in this way.

I have seen a number of SO posts related to tables not getting created and migrations not being applied, but I have not found anything that works for me yet.

  • My table is not an "unmanaged" table.

  • I have tried deleting all my migrations and generating new ones.

  • I have tried running
    for the entire project as well as running migrations for the specific app and even specific migrations

My assumption is that there is something in my database itself which is telling Django to not apply the migrations, but I don't know what to look for.

Any suggestions would be appreciated. Thank you.


When you succesfully run a migration, django stores a log of that migration in the django_migrations table (you can check it directly in your database) so the next time you try to run the same migration, django will see in the logs that you already run it once and it wont try to create the table again.

You could try to clean the applied migrations by modifying that table and then run the migrations again OR [recommended] go back to a safe point by using --fake:

let's say that you had problems with migration 0003 and 0002 but migration 0001 was ok... so to go back to migration 0001 do

./manage.py migrate my_app 0001 --fake

it will remove the 0002 and 0003 migrations on the django_migrations table and you'll be able to recreate them or run the new migrations again

Please do a backup of your database before testing this :P don't want to be responsible for any data loss XD

Hope this helps