Search code examples
djangodjango-south

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?


Solution

  • 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!