Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate a deleted table with Django Migrations?

There are two models Groups and Students and only one table for Groups of them, the Students table was deleted.

How to make Django recreate the deleted table? If I do makemigrations it prints "No changes detected".

On admin page when I click on the Students table it throws an exception:

relation "students_students" does not exist 
like image 981
Егор Лебедев Avatar asked Oct 21 '15 12:10

Егор Лебедев


People also ask

What happens if I delete Django migrations?

Deleting migration files means losing your history. This historical info is recorded in the django_migrations table in your database. if you delete migration files, you will get dependency errors. So Don't try to lose your history by deleting your migration files.

How do I flush migrations in Django?

Another option is to use Django's manage.py command to clear the whole database for us. The command is python manage.py flush . Again, after using this command, we have to delete all the migrations folders and then make the new migrations.


1 Answers

In django 1.7 you can try:

1. Delete your migrations folder  2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.    You could alternatively just truncate this table.  3. python manage.py makemigrations  4. python manage.py migrate --fake 

If you are working in django 1.9.5 this is the 100 % solution for this problem:

1. Delete your migrations folder  2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.    You could alternatively just truncate this table.  3. python manage.py makemigrations app_name  4. python manage.py migrate 

This works 100% for me!

like image 87
Raúl EL Avatar answered Oct 13 '22 05:10

Raúl EL