Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pesky "Table 'my_table' already exists" in Django-South

In Django-South: I changed I've run the initial migration successfully for myapp but for some reason, after I've made a change to my model and go to

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

And I get a lot of traceback which ends in:

(1050, "Table 'my_table' already exists")

After much googling, I found and tried this:

./manage.py migrate myapp --fake

And then I proceed to migrate it, to no avail; same error.

Any suggestions?

like image 851
JEEND0 Avatar asked Mar 11 '11 21:03

JEEND0


2 Answers

I just got this same error, and found this question by search.

My problem was that my second migration I'd created using the --initial flag, i.e.

$ ./manage.py startapp foo
$ ./manage.py schemamigration --initial foo
$ ./manage.py migrate foo

... make some changes to foo ...

$ ./manage.py schemamigration --initial foo

(oops!)

$ ./manage.py migrate foo

... and I get the error, and the migration fails because in the second migration, South is trying to create a table its already created.

Solution

In my migrations folder:

$ ls foo/migrations
0001_initial.py   0002_initial.py

remove that second migration and re-export the second migration with the correct --auto flag:

$ rm foo/migrations/0002_initial.py
$ ./manage.py schemamigration --auto foo
$ ./manage.py migrate foo

Success!

There may be other things that cause this error, but that was my bad!

like image 143
Ian Avatar answered Nov 18 '22 13:11

Ian


Is it an existing app?

In that case you will need to convert it in addition to the fake bit.

There are good docs here on converting an existing app.

Although they are quite tricky to find if you don't know where they are already ( ;

For converting, after adding south to your installed apps:

./manage.py syncdb
./manage.py convert_to_south myapp
./manage.py migrate myapp 0001 --fake
like image 32
Tom Gruner Avatar answered Nov 18 '22 11:11

Tom Gruner