8

I have changed the Django models, and I use the Django schemamigration to update the database. But, when I do python manager.py migrate app, it throws this error message:

_mysql_exceptions.OperationalError: (1050, "Table 'forum_user' already exists")
2
  • 1
    Then the table that django south is trying to create already exists and doesn't match the state of your database. If this is the first time you're migrating, remember that before you make schemamigration changes, you must set the initial state and migrate app --fake to match the database to the south database state Feb 21, 2012 at 7:50
  • how to set the initial state ?
    – mike
    Feb 21, 2012 at 7:51

2 Answers 2

29

Then the table that django south is trying to create already exists and doesn't match the state of your database.

If this is the first time you're migrating, remember that before you make schemamigration changes, you must set the initial state via schemamigration myapp --initial and migrate app --fake to match the database to the south database state.

manage.py convert_to_south myapp also does the above as a convenience method.

To start using south...

  1. Make sure your django tables match your current database tables exactly - if you planned to add or remove columns, comment those out.
  2. Run python manage.py schemamigration myapp --initial
  3. Run python manage.py migrate myapp --fake
  4. Make changes to your django model
  5. Run python manage.py schemamigration myapp --auto
  6. Run python manage.py migrate myapp

Update

Note django 1.7+ ships with migrations and south is no longer in use.

There are only two commands worth noting..

  • manage.py makemigrations (handles --initial)
  • manage.py migrate

Written by the same author as South, crowd funded. Go django.

2
  • This deserves way more up votes than it's got, valuable information and really helped.
    – Llanilek
    May 24, 2014 at 11:45
  • After reading multiple tutorials, your answer was the one which helped me fix my problem. Thank you. Jul 17, 2014 at 18:26
0

I just fixed a duplicate table issue locally and wanted to document my workflow to help others.

The key to success was creating an --empty migration before the new models are added. The flow:

  • Merged in another persons work that excluded info on a model I have locally.
  • normal schemamigration --auto was adding a table/model again and caused "already exists error".
  • Solved by commenting out the new model and running an empty migration via clear; python manage.py schemamigration --empty APPNAME MIGRATION_FILE_NAME. This creates a "declaration" of the state of the models with no forward/backwards commands. Be 100% sure the current state of models (python files) and database are in sync!!! This most current migration will be used for the creation of a differential to migrate correctly (next).
  • uncomment the new model and run clear; python manage.py schemamigration APPNAME --auto to create the true and desired differential off (uses the --empty migration just created). The new migration will have forward/backward commands that should be appropriate for your new model. Review...
  • finish off with clear; python manage.py migrate

The lesson learned is that --auto looks at the last APP+migration file to create a forward/backwards diff. If the last migration does NOT have in the dictionary a model you have in DB it will be created again causing an "already exists" error. Think of the dictionary as a contract between Django and the DB stating "here's what everything should look like". The migration can include commands that create duplicate tables and will only be exposed during ```migrate`` command.

The above info should fix the problem. Presented in part to help people and also for review in case I am doing something foolish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.