我最近将 South 添加到现有的 Django 项目中。我经历了整个
python manage.py syncdb
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake
根据这张票的最后一条评论处理(因为我有一个自定义用户模型)。
然后我想我做了一个架构迁移和迁移?我不完全记得,但我最终得到的是两个名为0001_initial.py
and的迁移文件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
,但似乎无法通过第二次迁移。我应该/可以只删除第二个迁移文件吗?它与第一个完全相同,这似乎是导致问题的原因,但我在南方并不精通,不知道这是否是个好主意。