Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django 1.7 migrations -- how do I clear all migrations and start over from scratch?

So I'm rapidly iterating on a django app at the moment and I'm constantly adjusting models.py. Over the course of a day or two of programming and testing I generate a couple dozen migration files. Sometimes I really tear the schema apart and completely re-do it. This causes the migration process to complain a great deal about defaults and null values and so on. If possible, I would just like to scratch all the migration stuff and re-start the migrations now that I finally know what I'm doing. My approach thus far has been the following:

  1. delete everything in the migrations folder except for __init__.py.
  2. drop into my PostgreSQL console and do: DELETE FROM south_migrationhistory WHERE app_name='my_app';
  3. while at the PostgreSQL console, drop all of the tables associated with my_app.
  4. re-run ./manage.py makemigrations my_app - this generates a 0001_initial.py file in my migrations folder.
  5. run ./manage migrate my_app - I expect this command to re-build all my tables, but instead it says: "No migrations to apply."

What gives?

Also, is the south_migrationhistory database table still in play now that I've dumped South and have switched to Django 1.7?

Thanks.

like image 917
tadasajon Avatar asked Oct 09 '14 16:10

tadasajon


People also ask

How do I reset Django migrations?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .

How do I delete all migrations?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What happens if I delete Django migrations?

Deleting migration files means losing your history. This historical info is recorded in the django_migrations table in your database. if you delete migration files, you will get dependency errors. So Don't try to lose your history by deleting your migration files.


1 Answers

So the step-by-step plan I outlined in my question does work, but instead of deleting rows from the south_migrationhistory database table, I had to delete rows from the django_migrations database table.

The command is: DELETE FROM django_migrations WHERE app='my_app'

Once this is done, you will be able to re-run your migrations from scratch.

like image 161
tadasajon Avatar answered Sep 21 '22 22:09

tadasajon