How to reset migrations in Django 1.7

二次信任 提交于 2019-11-27 11:22:34

I would just do the following on both the environments (as long as the code is the same)

  1. Delete your migrations folder
  2. DELETE FROM django_migrations WHERE app = <your app name> . You could alternatively just truncate this table.
  3. python manage.py makemigrations
  4. python manage.py migrate --fake

After this all your changes should get detected across environments.

Run

python manage.py migrate your_app zero

This will drop all tables from your_app

If you want, since you said you want to start over, you can delete your migrations folder, or maybe rename the folder, create a new migrations folder and run

python manage.py makemigrations your_app
python manage.py migrate your_app

Just like south, you can always go back and forth...

# Go to the first migration
python manage.py migrate your_app 0001

# Go to the third migration
python manage.py migrate your_app 0003

So imagine that your 4th migration is a mess... you can always migrate to the 3rd, remove the 4th migration file and do it again.

Note:

This one of the reasons your models should be in different apps. Say you have 2 models : User and Note. It's a good practice to create 2 apps: users and notes so the migrations are independent from each other.

Try not use a single application for all your models

A minor variation on harshil's answer:

$ manage.py migrate --fake <appname> zero
$ rm -rf migrations
$ manage.py makemigrations <appname>
$ manage.py migrate --fake <appname>

This will ...

  • pretend to rollback all of your migrations without touching the actual tables in the app
  • remove your existing migration scripts for the app
  • create a new initial migration for the app
  • fake a migration to the initial migration for the app

1)

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;

2)

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

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

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

3)

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 manage.py makemigrations
python manage.py migrate

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

When your 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 :

1)

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

python manage.py migrate --fake myApp zero

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

2)

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 init.py .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 "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

You can easily verify your migrations using :

python manage.py showmigrations

3)

Next we need create the migrations again so just execute

python manage.py 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 manage.py migrate --fake-initial 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!