我最近在一个现有的Django项目中添加了South。我经历了整个过程。
python manage.py syncdb
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake
根据这个票据上最新的评论进行处理(因为我有自定义用户模型)。
然后,我认为我执行了schemamigration和migrate?我不是完全记得,但最终我得到了两个迁移文件,分别为
0001_initial.py
和0002_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,不知道是否明智。