How to recover deleted table with Django Migrations?

There are two models of groups and students, and only one table for groups, the "Students" table has been deleted.

How to create Django again? If I do makemigrations , it will print "No changes detected."

On the admin page, when I click on the "Students" table, an exception occurs:

 relation "students_students" does not exist 
+13
python django postgresql django-models django-migrations
source share
9 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 running django 1.9.5, this is a 100% solution to 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 

It works 100% for me!

+38
source share

There is no easy way to get Django to recreate a table that you manually deleted. Once your database is manually modified, the Django view of the database (from migrations) is different from reality, and it can be difficult to fix.

If you run the sqlmigrate command, it will show you the necessary SQL to create the table. You can run SQL in the database shell. Assuming your application name is students , and the migration that created the table was 00XX_create_students.py , you should do:

 ./manage.py sqlmigrate students 00XX_create_students 

Be careful if there are foreign keys for or from the student table, you must also create restrictions.

+9
source share

For django 2 do this:

  1. Delete your migrations folder 2. In the database: 'DELETE FROM django_migrations WHERE app = 'app_name';' 3. python manage.py makemigrations app_name 4. python manage.py migrate 5. python manage.py migrate --fake default //This was the key to solve my problem 
+5
source share

I create the table manually and it helps.

0
source share

For Django 1.10.4 I deleted the db.sqlite3 file from the project folder and executed the following commands:

  • python manage.py makemigrations app_name
  • python manage.py migrate
0
source share

Django 1.11.2 using MariaDB, accidentally dropping a database table. To recreate the table, try the following: 1 / Delete everything except init .py in the application / migration directory 2 / select * from django_migrations; remove from django_migrations where app = 'yourapp'; 3 / Verify that your model is good and running: python manage.py makemigrations 4 / python manage.py migrate

It works for me!

0
source share

I just deleted the migration folder, dumped the entire database, and then did the migration for the application

 python3 manage.py makemigration python3 manage.py migrate 

and he came back.

0
source share

Rename the name of the remote table to some_new_name in models.py and run:

python3 manage.py makemigration
python3 manage.py migrate

rename the some_new_name table again to the original name and run

python3 manage.py makemigration
python3 manage.py migrate

finally go to dbshell and delete some_new_name table

0
source share

The answer that worked for me is this:

Suppose your model table has been deleted in your database and you need to recreate it, and then follow these steps.

  • comment out the model in models.py, which creates the remote table (either the model class, or the row that creates the table, like a = models.ManyToManyField(...) )

  • run: python manage.py makemigrations <app-name> , where <app-name> is the name of the application in which you have models.py

  • run: python manage.py migrate --fake <app-name>

  • comment out the model in models.py

  • run: python manage.py makemigrations <app-name>

  • run: python manage.py migrate <app-name> (without --fake)

and you should return to the database at the table. But any data that was in the table will be lost.

0
source share

All Articles