How to recreate a deleted table with Django Migrations?

 ̄綄美尐妖づ 提交于 2019-11-27 10:57:54

问题


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

How to maje Django create it again? 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

回答1:


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!




回答2:


There isn't an easy way to get Django to recreate a table that you have deleted manually. Once your database is altered manually, Django's view of the database (from migrations) is different from reality, and it can be tricky to fix.

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

./manage.py sqlmigrate students 00XX_create_students

Be careful if there are foreign keys to or from the students table, the constraints will have to be created as well.




回答3:


I create table manualy and it helps.




回答4:


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

  1. python manage.py makemigrations app_name
  2. python manage.py migrate



回答5:


Django 1.11.2 using MariaDB, accidental drop of database table. To recreate table, try the following: 1/ Delete all except for init.py in your app/migrations directory 2/ select * from django_migrations; delete from django_migrations where app = 'yourapp'; 3/ Check your model is good and run: python manage.py makemigrations 4/ python manage.py migrate

Works for me!




回答6:


I just deleted my migrations folder, dropped the whole database, then i made migration for the app

python3 manage.py makemigration 
python3 manage.py migrate

and it came back.




回答7:


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

python3 manage.py makemigration
python3 manage.py migrate

again rename the some_new_name table to the original name and run

python3 manage.py makemigration
python3 manage.py migrate

finally, go to the dbshell and drop the table some_new_name




回答8:


The answer that worked for me is as follows:

Assume in your database a table of a model has been deleted and you need to re-create, then do the following.

  • comment out the model in models.py that creates the table that has been deleted (either the model class or a line that creates a table like a = models.ManyToManyField(...))

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

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

  • un-comment the model in models.py

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

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

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




回答9:


just run this command:

python manage.py migrate --run-syncdb



回答10:


The only way that worked for me:

rm -r <app-name>/migrations/
python manage.py makemigrations <app-name>
python manage.py sqlmigrate <app-name> 0001_initial

Copy what it prints out (or, depending on what you have actually removed from the DB, only part of the SQL queries).

Apply those copied queries to your DB:

psql -U user_name -h 127.0.0.1 database_name

Paste what you have copied from the SQL queries printout.

Commit the queries.

And that's it - your missing tables are created.



来源:https://stackoverflow.com/questions/33259477/how-to-recreate-a-deleted-table-with-django-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!