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 makemigrations
and migrate
commands without issue. However, whenever I try to visit the site, I get the ProgrammingError
saying that my table topspots_notification
does not exist.
I have the following migration file in my migrations
folder:
# -*- 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 = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('topspots', '0018_siteuser_share_location'),
]
operations = [
migrations.CreateModel(
name='Notification',
fields=[
('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 notification
table is created), and every model that comes after it. Then I run makemigrations
and migrate
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.
migrate
for the entire project as well as running migrations for the specific app and even specific migrationsMy 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