Ycon
Ycon

Reputation: 1950

Deleted migration files- how to makemigrations without losing data

I changed a field on a model class (which has no classes point to it, only one foreign key pointing out of it). Somehow, this stuffed up my migrations and it keeps saying "django.db.migrations.graph.NodeNotFoundError:" looking for migration files that do not exist.

I accidentally deleted several files in my 'migrations' folder.

My database contains a lot of data, and I do not want to break it.

Will I lose any data if I:

  1. Remove the table that caused the problem in the first place (psql, \d, DROP TABLE tablename)

  2. delete all my migration files

  3. Re run the migration from the start?

    ./manage.py makemigrations

    ./manage.py migrate

Can anyone recommend another way of fixing this?

Here is the traceback: http://dpaste.com/0Y1YDXS

Upvotes: 4

Views: 10345

Answers (2)

  1. Empty the django_migrations table:

    delete from django_migrations;
    
  2. Remove all the files in migrations folders in each and every app of your project.

  3. Reset the migrations for the "built-in" apps:

    python manage.py migrate --fake
    
  4. Create initial migrations for each and every app:

    python manage.py makemigrations your_app_name
    
  5. Final step is to create fake initial migrations:

    python manage.py migrate --fake-initial
    

See here https://micropyramid.com/blog/how-to-create-initial-django-migrations-for-existing-schema/

Upvotes: 5

Remi Smirra
Remi Smirra

Reputation: 2539

Aren't you using git so that you can get your migration files back? If not, install and use it, starting now

I would suggest:

  1. make a backup/dump of your database first, in case something goes wrong
  2. Delete all migrations
  3. Empty migration table in psql
  4. call makemigrations
  5. call migrate --fake-initial

Upvotes: 6

Related Questions