9

In my project directory while I was trying to update my code I have accidentally remove the migrations folder of my app, now that I have modified the models and when I use python manage.py makemigrations I get the following message:

Operations to perform:
    Apply all migrations: app_label
Running migrations:
   No migrations to apply.

I have already run this before migrating python manage.py makemigrations app_label

samix73
  • 2,802
  • 4
  • 17
  • 29
  • Are you using version control? Can you just reset/checkout back to when you had the migrations? – jonrsharpe Jun 02 '16 at 22:41
  • @jonrsharpe unfortunately I'm on VPS and I still didn't get the time to install git server and I have migrations but for some reason when I place them up on server they give me the error that table exists – samix73 Jun 02 '16 at 22:47
  • And that's why you make the time to install that first! – jonrsharpe Jun 02 '16 at 22:48
  • 2
    @jonrsharpe ok now what's your solution to solve the problem? – samix73 Jun 02 '16 at 23:30

2 Answers2

12

Just create new directory called migrations inside your app directory. Then inside that folder create empty folder named __pycache__ and a empty file named __init__.py. Then just make the migrations and migrate

blagae
  • 2,342
  • 1
  • 27
  • 48
10

You could just reset your migrations to before your initial migrate and start over. This doesn't delete data in the database, but rather resets the tracking of your migrations. If all of your databases are already migrated to the same level, then you can start over in your migration tracking.

There is a StackOverflow question here that already addresses it:

How to reset migrations in Django 1.7?

In short,

./manage.py migrate --fake <app-name> zero

This resets the tracking to prior to your initial migration.

And then,

./manage.py makemigrations <app-name>
./manage.py migrate <app-name>

Which recreates the initial migration and applies it.

As always, if your data is important, make a backup first.

Community
  • 1
  • 1
rrauenza
  • 6,285
  • 4
  • 32
  • 57
  • I actually need the data in the database – samix73 Jun 02 '16 at 23:30
  • I've updated my answer. Resetting your migrations doesn't reset your database. – rrauenza Jun 02 '16 at 23:37
  • after running the command given above I got this message and nothing else actually happened `Running migrations: Rendering model states... DONE Unapplying app_label.0001_initial... OK` – samix73 Jun 02 '16 at 23:40
  • 1
    This exception is now thrown relation "app_label_something" does not exist – samix73 Jun 02 '16 at 23:42
  • You should now be able to proceed as you did when you initially started. – rrauenza Jun 02 '16 at 23:42
  • It would be helpful if you described what you ran to get that exception, and what `_something` is from. Did you remake your migrations? – rrauenza Jun 02 '16 at 23:51
  • something is the name of another model related with foreignkey and yes I have removed migrations directory and ran makemigraions on the app again – samix73 Jun 02 '16 at 23:53
  • problem is solved didn't run `makemigrations` and `migrate` after resetting migrations. Thank you – samix73 Jun 03 '16 at 00:04
  • I've updated to make the process clearer. 'reset' to me means back to the beginning of time -- prior to initial migration. That should be clearer now. – rrauenza Jun 03 '16 at 00:07
  • The order of execution may need to be changed as per the requirement. But it helps me a lot. Thanks much :) – Parvathirajan Natarajan Jul 02 '20 at 17:13