简体   繁体   English

django 迁移 - 重新创建数据库

[英]django migration - re-creating the database

I am running python 2.7 and django 1.8.我正在运行 python 2.7 和 django 1.8。

I have this exact issue. 我有这个确切的问题。

The answer, posted as a comment is: What I did is completely remake the db, erase the migration history and folders.作为评论发布的答案是: What I did is completely remake the db, erase the migration history and folders.

I am very uncertain about deleting and creating the database.我对删除和创建数据库非常不确定。

I am running a PostgreSQL database.我正在运行 PostgreSQL 数据库。 If I drop/delete the database and then run the migrations, will the database be rebuilt from the migrations?如果我删除/删除数据库然后运行迁移,数据库是否会从迁移中重建? I don't want to delete the database and then be stuck in a worse situation.我不想删除数据库然后陷入更糟糕的情况。

Here is what I did to correct this issue:这是我为纠正此问题所做的工作:

First, make a script of the data, so that when the database is destroyed and re-created, the data can be easily re-created.首先,对数据做一个脚本,这样当数据库被破坏和重新创建时,数据可以很容易地重新创建。

Next run the following script to reset the database (this will delete all the db data and remove the db tables).接下来运行以下脚本以重置数据库(这将删除所有 db 数据并删除 db 表)。

Then run the following script to recreate the db:然后运行以下脚本来重新创建数据库:

python manage.py reset_db

Then, run the following script to re-create the db:然后,运行以下脚本以重新创建数据库:

python manage.py migrate

Next, run the script to create the database data.接下来,运行脚本以创建数据库数据。

Finially, run the following script to start the server:最后,运行以下脚本来启动服务器:

python manage.py runserver

That is it.就是这样。

I hope that this helps some one out there.我希望这对那里的人有所帮助。

In fact there is no need to do that, but in order to make sure you are doing right take following things into consideration:事实上,没有必要这样做,但为了确保您做得对,请考虑以下事项:

  1. Make sure your DB is not being accessed (Written) by a process or your app during the migrations process.确保您的数据库在迁移过程中未被进程或您的应用程序访问(写入)。 I had a lot of troubles with that.我有很多麻烦。

  2. If your server is running, make sure to turn it off.如果您的服务器正在运行,请确保将其关闭。 There are some issues that prevent the migrations or updating the results while the server is running, in particular in production mode.在服务器运行时,特别是在生产模式下,存在一些问题会阻止迁移或更新结果。

  3. If you are migrating in production after migrating in development mode, make sure that there are no files from the development.如果您在以开发模式迁移后在生产中迁移,请确保没有来自开发的文件。 They are name similarly, so the system throws an error about migrations already exist.它们的名称相似,因此系统会抛出关于迁移已经存在的错误。

  4. Remember about default values.记住默认值。 If you are migrating in order to include a new column in a pre-existent table, you must provide a default value to fill those rows in the new column.如果您迁移是为了在预先存在的表中包含新列,则必须提供默认值以填充新列中的这些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django South-从sqlite3删除表后,迁移未重新创建模型 - Django South - Migration not re-creating model after dropping table from sqlite3 阻止Django重新创建默认权限 - Prevent Django from re-creating default permissions 重新创建一个python调用 - Re-creating a python invocation 获取重新创建 object 所需的导入 - Get imports required for re-creating an object 如何在不重新创建的情况下更改BarGraphItem的颜色? - How to change color of a BarGraphItem without re-creating? 在日益流行的语言中重新创建线程和并发知识 - Re-creating threading and concurrency knowledge in increasingly popular languages 销毁并重新创建使用 FigureCanvasTkAgg 实现的 matplotlib 图 - Destroying and re-creating a matplotlib diagram implemented with FigureCanvasTkAgg 在Python中通过Apple Numbers应用重新创建多项式 - Re-Creating Polynomial From Apple's Numbers App in Python Django在删除迁移文件和迁移表后未创建数据库表 - Django not creating database table after deleting migrations files and migration table 无法在不重新创建我的分组 dataframe 的情况下多次使用子图 - Unable to use subplot more than once without re-creating my grouped dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM