reset migrations in Django 1.7 – 1.8 – 1.9 and above

reset migrations in Django 1.7 – 1.8 – 1.9 and above

In this post we will give you information about reset migrations in Django 1.7 – 1.8 – 1.9 and above. Hear we will give you detail about reset migrations in Django 1.7 – 1.8 – 1.9 and aboveAnd how to use it also give you demo for it if it is necessary.

Migrations help you propagate models changes to your database schema,they are particularly helpful in the situation when you need to change your database structure and you don’t want or you can’t drop a database table and recreate it or when you have a production database with tables which has millions of rows .Any developer has experienced situations where he has to change the structure of an existing table such as adding,deleting or renaming a field.In some cases droping the table and recreate it solve the problem and release the developer from the headache related to and resulted by the process but just imagine a scenario where your application is already in production with millions of database rows ,droping your old tables is not a choice.You can’t even dear to think about it so migrations are here to present you a more acceptable and professional solution.

Simply migrations lets you change your database schema while keeping your data.

How to get started with migrations ?

Getting started with migrations is easy especially with the latest versions of Django,starting with Django 1.7 .In fact from Django 1.7 migrations become obligatory since they are integrated within your django workflow.

To work with migrations Django has two essential commands for you

python makemigrations  # create migrations filespython migrate # apply migrations and create actual database tables

You can also show your migrations with

python showmigrations #show migrations

How to reset django migrations ?

The django migrations system is very powerful,flexible and designed to handle big number of migrations but having a lot of models migrations causes problems when frequently changing your database structure(during development phase in most cases ) to most django developers both biginners and even experienced .For many situations you’ll have no solution at all but to clean up and reset the migrations so how can you reset Django migrations when things gets really messed up ?

The solution is simple but depends on whether you are still on development phase or you are on production so lets detail the process :

Resetting Django migrations in development phase

So you are still developing the application ,you app is not on production yet .In this case deleting the whole database is not a big deal.You can reset the migrations in exactly three steps


Start by droping your database using SQL language if you are using a database system like Posgres or MySQL and you have a database with name mydb

delete mydb;

If you are using sqlite you just need to delete sqlite file which’s generally named db.sqlite3


Next you need to go through all migrations folders for each app that belongs to your project and delete the migrations files except for

If you are using a terminal under Linux/MAC you can automate this tedious easilly .For example

find . -path "*/migrations/*.py" -not -name "" -deletefind . -path "*/migrations/*.pyc"  -delete


Next and last all you have to do is execute the two migrations command you normally invoke when you are syncing your models to a new database with Django

python makemigrationspython migrate

Make sure you have created a new database after deleting the old one if you are using any database system other than sqlite.

That’s all for the first case i.e when you are still developing your app and not yet on production mode.That’s actually very easy and makes no problem at all but things becomes more complex when you are app is on production because you can’t drop your database so how can you reset migrations in this case ?

We simply need to keep the database while getting rid of migrations history .Here is how you can do it in detailed steps :


First go through each app and detete its migrations history by ussuing the following command

python migrate --fake myApp zero

Django will unapply any previous migrations for the specific app(myApp)


Next you need to actually delete the migrations file so you’ll need again to go through each app migrations folder and delete them except for .Simply use the folloing script under any unix based operating system.For Windows you can use the power commandd line .It should be similar to unix but sincerly i don’t use Windows and netier power bash

find . -path "*/migrations/*.py" -not -name "" -deletefind . -path "*/migrations/*.pyc"  -delete

You can easilly verify your migrations using :

python showmigrations


Next we need create the migrations again so just execute

python makemigrations

BUT don’t forget the database still have tables belonging to initial migrations so what we need to do is migrating our database and in the same time faking the initial migartions you can do that by simply execute

python migrate --fake-initial     

So I hope you have completed the step successfully .

Using a Django app for resetting migrations

Instead of going through all these steps you can use a recently developed Django app to reset migrations in a few commands.

The app does all steps for such as deleting the migration files,clraring django_migrations table and then recreating migrations and faking old migrations.

So first you need to install the app via pip

pip install django-reset-migrations

Then add the app in your INSTALLED_APPS under

INSTALLED_APPS = (    # ...    'reset_migrations',    # ...)

Next you can simply start using it from the CLI

python reset_migration myApp

You can also specify multiple apps for reset

python reset_migration myApp myOtherApp

In case you don’t want to delete migrations files of your app(s) just add –cached

python reset_migration myApp myOtherApp --cached  

Manually modifying the migrations

As you cab notice migrations files are just Python files so you can consider manually modifying your migrations to get rid of any conflicts .Django documenation offers great explanation about how to write migrations yourself{:rel=”nofollow”} so don’t be afraid of migrations they are actually easy to control when you get familiar with how they work.

Merging the migrations

Sometimes it’s also possible to solve migrations issues by just merging them in that case Django will ask you to do that but that works only for simple models modifications.

When you appling your migrations with

python migrate    

Django detects that conflicts exist so it will instrcut you to use python makemigrations –merge so follow the orders of your master and execute

python makemigrations –merge

Then just migrate again

python migrate

But unfortunalty that works ony for simple modifications of your models.


So that’s the end of this short tutorial so remember don’t be afraid of migrations .They are really helpfull despite the headache that they may make but that’s something that’s you’ll learn to manage after becoming more experienced with Django and also by undersatnding how migrations work so you can avoid introducing any conflicts when changing your models.

Hope this code and post will helped you for implement reset migrations in Django 1.7 – 1.8 – 1.9 and above. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US