Python 如何使用 Django Migrations 重新创建已删除的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33259477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:06:34  来源:igfitidea点击:

How to recreate a deleted table with Django Migrations?

pythondjangopostgresqldjango-modelsdjango-migrations

提问by Егор Лебедев

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

有两个模型Groups和Students,其中Groups只有一张表,Students表被删除了。

How to maje Django create it again? If I do makemigrationsit prints "No changes detected".

如何让 Django 再次创建它?如果我这样做,makemigrations它会打印“未检测到更改”。

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

在管理页面上,当我单击学生表时,它会引发异常:

relation "students_students" does not exist

采纳答案by Raúl EL

In django 1.7 you can try:

在 Django 1.7 中,您可以尝试:

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:

如果您在 django 1.9.5 中工作,这是此问题的 100% 解决方案:

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!

这对我来说 100% 有效!

回答by Alasdair

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.

没有一种简单的方法可以让 Django 重新创建您手动删除的表。一旦您的数据库被手动更改,Django 的数据库视图(来自迁移)就与现实不同,并且修复起来可能很棘手。

If you run the sqlmigratecommand, 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:

如果您运行sqlmigrate命令,它将向您显示创建表所需的 SQL。您可以在数据库 shell 中运行 sql。假设您的应用程序名称是students,并且创建表的迁移是00XX_create_students.py,您将执行以下操作:

./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.

如果有进出学生表的外键,请小心,还必须创建约束。

回答by Егор Лебедев

I create table manualy and it helps.

我手动创建表格,它有帮助。

回答by siddharth

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

对于 Django 1.10.4,我从项目文件夹中删除了 db.sqlite3 文件,然后运行以下命令:

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

回答by user2519486

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

Django 1.11.2 使用 MariaDB,数据库表意外删除。要重新创建表,请尝试以下操作: 1/ 删除app/migrations 目录中除init.py之外的所有内容2/ select * from django_migrations; 从 django_migrations 中删除 app = 'yourapp'; 3/ 检查您的模型是否良好并运行: python manage.py makemigrations 4/ python manage.py migrate

Works for me!

为我工作!

回答by Andrews Agyemang Opoku

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.

它回来了。

回答by Programming-Lover

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

在models.py中将删除的表名重命名为some_new_name并运行:

python3 manage.py makemigration
python3 manage.py migrate

python3 manage.py makemigration
python3 manage.py migrate

again rename the some_new_name table to the original name and run

再次将 some_new_name 表重命名为原始名称并运行

python3 manage.py makemigration
python3 manage.py migrate

python3 manage.py makemigration
python3 manage.py migrate

finally, go to the dbshell and drop the table some_new_name

最后,转到 dbshel​​l 并删除表 some_new_name

回答by Bruno Vermeulen

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.

假设你的数据库中有一个model的表被删除了,需要重新创建,然后进行如下操作。

  • 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)

  • 注释掉models.py中创建已删除表的模型(模型类或创建表的行a = models.ManyToManyField(...)

  • 运行:python manage.py makemigrations <app-name>,其中<app-name>是您拥有models.py的应用程序的名称

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

  • 在models.py中取消对模型的注释

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

  • 运行:(python manage.py migrate <app-name>没有--fake)

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

你的表应该回到数据库中。但是表中的任何数据都将丢失。

回答by Ozzy

just run this command:

只需运行此命令:

python manage.py migrate --run-syncdb

回答by Airstriker

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).

复制它打印出来的内容(或者,根据您实际从数据库中删除的内容,只复制部分 SQL 查询)。

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.

粘贴您从 SQL 查询打印输出中复制的内容。

Commit the queries.

提交查询。

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

就是这样 - 您丢失的表已创建。