为什么南迁移失败?

时间:2012-10-19 11:27:18

标签: django django-south

我有一个我刚创建的空白MySQL数据库。 south位于INSTALLED_APPS

我跑:

$ ./manage.py syncdb
...
Creating table some_app_table

You just installed Django's auth system, which means you don't have any superusers defined.
...
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
...

Not synced (use migrations):
 - myapp
...


$ ./manage.py schemamigration myapp --initial
 + Added model myapp.Model
...
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp


$ ./manage.py migrate myapp
Running migrations for myapp:
 - Migrating forwards to 0003_initial.
 > skan:0001_initial
 > skan:0002_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE `myapp_model` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `user_id` integer NULL, `name` varchar(200) NOT NULL);
The error was: (1050, "Table 'myapp_model' already exists")

发生了什么事?南方为什么不能正确初始化?

1 个答案:

答案 0 :(得分:5)

您已经定义了一些迁移:initial(正如预期的那样)仅用于初始迁移。

您的syncdb输出显示:

Not synced (use migrations):
 - myapp

这表明南方正在按预期运作。但是,那么你做了:

$ ./manage.py schemamigration myapp --initial
+ Added model myapp.Model
...
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp

请注意0003 - 前缀:此(很可能)表示已定义迁移。这可以通过下一个命令的输出来确认:

$ ./manage.py migrate myapp
Running migrations for myapp:
 - Migrating forwards to 0003_initial.
 > skan:0001_initial
 > skan:0002_initial
 <snip>

换句话说,您已经进行了几次initial次迁移,其中至少有一次将创建该表。您的#3迁移再次尝试此操作,但失败了,因为该表当然存在。

您需要做的只是在创建Django应用时使用initial。只要migrations文件夹包含名为0001_initial.py的文件,您就不再需要任何初始迁移。如果您从此时开始更改表格,请使用auto进行调用,然后迁移:

./manage.py schemamigration myapp --auto
./manage.py migrate myapp