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.
I actually found an easier way to do this. You fake that you rollback what doesn't exist, then you re-migrate. If your migration 0005 was the one where it creates the table:
Should be good after that!
If you need to skip later ones, you do this:
Should be good after that!
The simplest way to do this on django >= 1.9 is to run the following:
That will remove your tables and revert all migrations.
Just ran into this while building a little app learning django. I wanted to create a non-null column for an existing table. There were three steps:
For an actual application you'd need to supply a default value as others have pointed out.
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:
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
Full disclaimer, this is a destructive operation in some cases, and I mostly use it to remigrate parts of the system without affecting the DB.
Have you tried doing it via the table
django_migrations
? Just remove the rows that map to the app label and the migration names in question and delete those rows.So now if i want to remove
homepage
, I can just delete row 30, 31, 32.Of course since you dropped the tables too, you'd need to change
django_content_type
too:So now you'd have to remove the tables that you need to remigrate need by dropping the rows for those tables.
I've used this when time was scarce and we needed a quick dirty fix, or when playing around in development.
Hope it helps you too!
OK, so what I did was not to mess with migrations. Seems like I get in trouble every so often with migrations. And in this case, trying to replay migrations got me nowhere. Might not have helped that there were some South-vintage migrations as well as the newer 1.7 stuff.
environment: postgres 9.3
Basically, I restored an old backup of my database into an empty database. Then I brought up the restore target in the postgres admin utility and copy/pasted the create tables from each table's description (I had only 4 to go). Switched over to my test database & ran it in pg's sql utility.
I dunno, I don't think it is unreasonable to drop a table manually if you are having issues with it (looked to me as if my id field's sequence was not working), as long as you can live with losing your data. Migrations should be resilient in that use case.