为什么南迁移失败?

fre*_*ley 1 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
...
Run Code Online (Sandbox Code Playgroud)


$ ./manage.py schemamigration myapp --initial
 + Added model myapp.Model
...
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp
Run Code Online (Sandbox Code Playgroud)


$ ./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")
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?南方为何不能正确初始化?

jro*_*jro 5

您已经定义了一些迁移:initial只是(正如预期的那样)只需要初始迁移.

你的syncdb输出说:

Not synced (use migrations):
 - myapp
Run Code Online (Sandbox Code Playgroud)

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

$ ./manage.py schemamigration myapp --initial
+ Added model myapp.Model
...
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp
Run Code Online (Sandbox Code Playgroud)

请注意0003-prefix:this(最有可能)表示已定义迁移.这可以通过下一个命令的输出来确认:

$ ./manage.py migrate myapp
Running migrations for myapp:
 - Migrating forwards to 0003_initial.
 > skan:0001_initial
 > skan:0002_initial
 <snip>
Run Code Online (Sandbox Code Playgroud)

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

你需要做的只是用于initial创建你的Django应用程序.只要migrations文件夹包含一个名为的文件0001_initial.py,就不再需要任何初始迁移.如果从此时开始更改表,请使用auto,然后迁移:

./manage.py schemamigration myapp --auto
./manage.py migrate myapp
Run Code Online (Sandbox Code Playgroud)