Using Django 1.7 migrations.
I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply".
How to I get Django to recreate the table?
I have run:
> makemigrations - No changes detected
> migrate - No migrations to apply.
I have tried making a change to the model and running a new migration and it simply states that "Table 'x.test_customer' doesn't exist" which is correct, but what I was hoping it that it would recreate the table.
Go to your database and find the table django_migrations
. Delete all the rows which have app
equals your app name.
Then do a makemigrations
& migrate
will work.
Another solution I've found and works perfectly:
In django 1.7:
Delete your migrations folder
In the database: DELETE FROM django_migrations WHERE app = 'app_name'
.
You could alternatively just truncate this table.
python manage.py makemigrations
python manage.py migrate --fake
In django 1.9.5:
Delete your migrations folder
In the database: DELETE FROM django_migrations WHERE app = 'app_name'
.
You could alternatively just truncate this table.
python manage.py makemigrations app_name
python manage.py migrate
This works 100% for me!
Migrations check for differences in your models, then translates that to actions, which are translated to SQL. It does not automatically sync the db scheme with your models, and it has no way of knowing you dropped a table (it doesn't know about manual changes because, well, you're not supposed to do manual changes. That's the point)
The answer? a manual change requires a manual migration as well. What you need to do is simply write your own migration and manually tell south to re-build the table. It's not very difficult, The docs make it pretty easy. Just make something like this:
from django.db import migrations, models
class Migration(migrations.Migration):
operations = [
migrations.CreateModel("Foo"),
migrations.AddField("Foo", "bar", models.IntegerField(default=0))
]
You can probably look into the first migration file (the one that made the model in the first place) and copy paste almost all of it. Then all you have to do is run the migration like you always do
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With