restore migration files

142 views
Skip to first unread message

Leif

unread,
Jun 6, 2018, 12:54:48 PM6/6/18
to Django users
Dear Django Experts:

Somehow I deleted the files under migrations directory. Now I am no longer able to migrate new changes. I got 'No change detected' message when I apply python manage.py migrate. Anway I can restore files under migrations directory?

Best,

Leif

Leif

unread,
Jun 6, 2018, 1:12:06 PM6/6/18
to Django users

Mike Dewhirst

unread,
Jun 6, 2018, 1:24:24 PM6/6/18
to django...@googlegroups.com
Maybe you can find an old backup of your models and create a new database from them. Then overwrite the old models with your latest ones and make migrations again. Those should suffice to replace the deleted migrations.

Connected by Motorola


Leif <xing...@gmail.com> wrote:

Dear Django Experts:

Somehow I deleted the files under migrations directory. Now I am no longer able to migrate new changes. I got 'No change detected' message when I apply python manage.py migrate. Anway I can restore files under migrations directory?

Best,

Leif

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7c972a46-5e24-44cb-bb27-b0b006197d8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

C. Kirby

unread,
Jun 6, 2018, 2:23:01 PM6/6/18
to Django users
If you don't need the history of the app migrations then it is pretty straightforward, you will basically end up with a squashed migration. If you do need the migration history (there is another system that is not up to sync with yours, etc) you are stuck, and I would suggest you start using a version control system (git, hg, etc)
Assuming it is ok to end up with one squashed migration:

1) Remove any changes you have made in your code that inform the database schema for the app(s) in question(so model changes)
2) Access the database directly (assuming postgres - psql, other databases have different interfaces)
3) There is a table django_migrations with the fields id, app, name, applied. Run a
delete from django_migrations where app = '';
for whatever app you deleted the migrations from (run it a few times if you deleted multiple app migrations)
4) For all the apps you need to rebuild migrations for
./manage makemigrations [app-names]
5)For  each app that you needed to rebuild the migrations
./mange migrate [app-name] --fake
This tells the database to write to the dajngo_migrations table that the migration was run, but doesn't actually change the schema. This is why your code and db need to be in sync. Otherwise you will have changes in the migration that are not reflected in the db, but the migtration is marked as having been run

You are now back in shape. Now set up git. I  am happy to help or point you to references for it

Kirby

On Wednesday, June 6, 2018 at 8:54:48 AM UTC-4, Leif wrote:

Leif

unread,
Jun 6, 2018, 2:49:07 PM6/6/18
to Django users
Hi Kirby,
Thank you very much. That is very helpful. It is working now. Now the migrations directory is tracked in code repository to prevent from happening again.
Best,
Leif  

Leif

unread,
Jun 6, 2018, 2:49:40 PM6/6/18
to Django users
thank you, Mike

C. Kirby

unread,
Jun 6, 2018, 2:55:36 PM6/6/18
to Django users
Glad to hear!
Reply all
Reply to author
Forward
0 new messages