1

我有一个刚刚创建的空白 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")

这是怎么回事?为什么 South 不能正确初始化?

4

1 回答 1

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-prefix:这(很可能)表明已经定义了迁移。下一个命令的输出证实了这一点:

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

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

What you need to do is only use initial on the creation of your Django app. As soon as the migrations folder contains a file called 0001_initial.py, you don't need any initial migrations anymore. If you change your table from this point on, call it with auto, and then migrate:

./manage.py schemamigration myapp --auto
./manage.py migrate myapp
于 2012-10-19T12:43:53.363 回答