gpt4 book ai didi

mysql - 使用south迁移数据库表

转载 作者:行者123 更新时间:2023-11-29 08:04:08 27 4
gpt4 key购买 nike

我没有使用南方。现在我想添加几列。我是不是完蛋了?

(env)noah:code broinjc$ ./manage.py schemamigration reports --initial
Creating migrations directory at '/Users/broinjc/esp/code/reports/migrations'...
Creating __init__.py in '/Users/broinjc/esp/code/reports/migrations'...
+ Added model reports.Classroom
+ Added model reports.Student
+ Added model reports.SurveySet
+ Added model reports.Survey
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate reports
(env)noah:code broinjc$ ./manage.py migrate reports
Running migrations for reports:
- Migrating forwards to 0001_initial.
> reports:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "reports_classroom" ("id" integer NOT NULL PRIMARY KEY, "user_id" integer NOT NULL, "added" datetime NOT NULL, "updated" datetime NOT NULL, "name" varchar(30) NOT NULL, "am_or_pm" varchar(2) NOT NULL)
The error was: table "reports_classroom" already exists
! Error found during real run of migration! Aborting.

! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.

! You *might* be able to recover with: = DROP TABLE "reports_classroom"; []
= DROP TABLE "reports_student"; []
= DROP TABLE "reports_surveyset"; []
= DROP TABLE "reports_survey"; []

! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: reports:0001_initial

看到这一切后,我想,也许我需要更新我的模型(使它们与 sqlite db 不一致),所以我更新了它们,然后运行相同的命令,但使用 --auto 而不是初始...

(env)noah:code broinjc$ ./manage.py schemamigration reports --auto
? The field 'SurveySet.top_num' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now

...所以我继续选择选项 2,然后继续迁移...

(env)noah:code broinjc$ ./manage.py migrate reports
Running migrations for reports:
- Migrating forwards to 0002_auto__add_field_surveyset_top_num__add_field_surveyset_externalizer_ra.
> reports:0001_initial

FATAL ERROR - The following SQL query failed: CREATE TABLE "reports_classroom" ("id" integer NOT NULL PRIMARY KEY, "user_id" integer NOT NULL, "added" datetime NOT NULL, "updated" datetime NOT NULL, "name" varchar(30) NOT NULL, "am_or_pm" varchar(2) NOT NULL)
The error was: table "reports_classroom" already exists
! Error found during real run of migration! Aborting.


! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.

! You *might* be able to recover with: = DROP TABLE "reports_classroom"; []
= DROP TABLE "reports_student"; []
= DROP TABLE "reports_surveyset"; []
= DROP TABLE "reports_survey"; []

最佳答案

我会尝试解释发生了什么,以便您更好地了解如何做您自己想做的事情。

在使用 South 之前,您的数据库中有一些表,这些表是您首次运行 syncdb 时从模型生成的。

如果您更改模型,例如添加一个字段“my_field”,Django 在尝试读取/写入该字段时将会失败,因为该表不包含名为“my_field”的列。您通常必须转储整个表并使用 syncdb 重新创建它。我确信您不想这样做,因为您的数据库中已经有一些数据。

假设您想要进行一些更改而不丢失数据。首先,您需要"convert" your app to south .

基本上,当您运行 schemamigration --initial 时,South 将创建一个脚本 (0001_initial.py) 将模型的当前状态复制到数据库中。

如果您通过manage.py migrate reports运行该脚本,它会尝试重新创建您最初拥有的所有表,但在您的情况下,由于您的数据库已经包含这些表,因此它'会对你尖叫说 table 已经存在:

FATAL ERROR - The following SQL query failed: CREATE TABLE "reports_classroom" ("id" integer NOT NULL PRIMARY KEY, "user_id" integer NOT NULL, "added" datetime NOT NULL, "updated" datetime NOT NULL, "name" varchar(30) NOT NULL, "am_or_pm" varchar(2) NOT NULL)
The error was: table "reports_classroom" already exists

要让 South 相信您已经应用了该迁移,请使用 --fake 选项。

manage.py 迁移报告 0001 --fake

这就像说,转到迁移状态 0001_initial(您只需编写名称的数字部分),但实际上并不应用更改。

完成此操作后,假设您将一个新字段“my_field_02”添加到您的模型之一。和以前一样,Django 引用了模型表中不存在的字段。要在不自己编写 SQL 的情况下创建它,您可以:

manage.py schemamigration reports --auto

这将创建一个名为 0002_auto__add_my_field_02.py 的新迁移,然后您需要通过 manage.py 迁移报告应用该迁移。您也可以说 manage.py migrate reports 0002 来指定您想要转到的迁移状态,但默认情况下 South 将尝试应用以下所有迁移(请记住您已经处于状态 0001) .

我强烈建议您阅读South's documentation并在执行任何操作之前备份您的生产数据。

tl;dr 阅读 this并备份您的数据。

关于mysql - 使用south迁移数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23072565/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com