fredley
fredley

Reputation: 33941

Why is south migrate failing?

I have a blank MySQL database that I've just created. south is in my INSTALLED_APPS.

I run:

$ ./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")

What's going on? Why won't South initialise correctly?

Upvotes: 1

Views: 1744

Answers (1)

jro
jro

Reputation: 9484

You already have some migrations defined: initial is (as expected) only needed for the initial migration.

Your syncdb output says:

Not synced (use migrations):
 - myapp

Which indicates that south is working as expected. But, then you do:

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

Notice the 0003-prefix: this (most likely) indicates that there are already migrations defined. This is confirmed by the output of your next command:

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

In other words, you already have a couple of initial migrations, of which at least one will create that table. Your #3 migration tries this again, but fails, because the table of course exists by now.

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

Upvotes: 5

Related Questions