南部数据库错误:关系已经存在。

4

我最近在一个现有的Django项目中添加了South。我经历了整个过程。

python manage.py syncdb
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake

根据这个票据上最新的评论进行处理(因为我有自定义用户模型)。
然后,我认为我执行了schemamigration和migrate?我不是完全记得,但最终我得到了两个迁移文件,分别为0001_initial.py0002_initial.py,这似乎并不完全正确。
今天,我尝试向我的一个模型中添加一个字段,并执行如下迁移:
 $ python manage.py schemamigration myapp --auto
 ? The field 'Photo.main_person' 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
 ? Please select a choice: 2
 ? Please enter Python code for your one-off default value.
 ? The datetime module is available, so you can do e.g. datetime.date.today()

 >>> 1
 + Added field main_person on myapp.Photo
Created 0003_auto__add_field_photo_main_person.py. You can now apply this migration with: ./manage.py migrate myapp

$ python manage.py migrate myapp
Running migrations for myapp:
 - Migrating forwards to 0003_auto__add_field_photo_main_person.
 > myapp:0002_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "myapp_patient" ("id" serial NOT NULL PRIMARY KEY, "password" varchar(128) NOT NULL, "last_login" timestamp with time zone NOT NULL, "email" varchar(255) NOT NULL UNIQUE, "first_name" varchar(100) NOT NULL, "last_name" varchar(100) NOT NULL, "is_active" boolean NOT NULL, "is_admin" boolean NOT NULL, "is_staff" boolean NOT NULL)
The error was: relation "myapp_patient" already exists

Error in migration: myapp:0002_initial
DatabaseError: relation "myapp_patient" already exists

因此,它创建了迁移0003_auto__add_field_photo_main_person,但似乎无法通过第二次迁移。我应该/可以删除第二个迁移文件吗?它与第一个完全相同,这似乎是问题的原因,但我不太熟悉South,不知道是否明智。

1个回答

9
你必须使用 --initial 创建了第二个迁移,所以名称为 002_initial.py。你应该使用 --auto 创建它。
在此过程中,South 认为第二个迁移是初始迁移,因为你传递了 --initial。因此,在迁移文件中编写命令时,期望表 myapp_patient 尚未创建。
另外,你现在不应该运行第二个迁移,否则你将在第一次尝试运行第二个迁移时遇到此错误。
如果第二个迁移与第一个迁移完全相同,则可以将其删除。
你还可以选择对第二个迁移进行 --fake 操作。
在虚拟第二个迁移之后,你只需执行通常的迁移即可。这样就可以解决问题了。 http://agiliq.com/blog/2012/01/south/ 可能有助于澄清 South 的一些概念 :)

只是为了明确起见:我可以直接删除0002_initial.py文件,不会发生任何问题,还是需要运行某个south命令? - snorthway
如果您是唯一在项目上工作的人,我建议使用“python manage.py migrate appname 0002 --fake”。我之所以说“如果您是唯一的”,是因为如果还有其他人在此工作,则需要告诉他们也要伪造第一和第二次迁移。 - Akshar Raaj
我是唯一在此工作的人,同时使用--fake也起作用了。非常感谢! - snorthway
@ashkar,再次感谢你;这个帮我省了很多麻烦和数据。 - The Pied Pipes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接