简体   繁体   中英

Schema migration with south (django) and MySQL

I was just going through a tutorial on django, and I followed every step, but still I get this error every time I try to migrate:

The initial migration works, but trying to migrate it, I get this:

  root@debian:/Sites/tumblog# ./manage.py migrate blog

   Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/pymodules/python2.6/south/management/commands/migrate.py", line 102, in handle
    delete_ghosts = delete_ghosts,
  File "/usr/lib/pymodules/python2.6/south/migration/__init__.py", line 202, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 215, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 284, in migrate_many
    result = self.migrate(migration, database)
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 121, in migrate
    result = self.run(migration)
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 95, in run
    return self.run_migration(migration)
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 77, in run_migration
    migration_function()
  File "/usr/lib/pymodules/python2.6/south/migration/migrators.py", line 56, in <lambda>
    return (lambda: direction(orm))
  File "/Site/tumblog/blog/migrations/0005_initial.py", line 17, in forwards
    ('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
  File "/usr/lib/pymodules/python2.6/south/db/generic.py", line 210, in create_table
    ', '.join([col for col in columns if col]),
  File "/usr/lib/pymodules/python2.6/south/db/generic.py", line 134, in execute
    cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1050, "Table 'blog_blog' already exists")

I've searched around and found out that adding --fake will skip this error, but it won't change a thing? What is the problem here and how can I fix it?

Are you sure adding --fake doesnt change anything ?

manage.py migrate blog --fake

... or drop table 'blog_blog' manually from mysql console ;)

The problem is that the tables the migration is trying to create already exist in the database. They must have been created with a syncdb (you get this if you do syncdb and later start using south for the app), or by a previous migration.

It's suspicious the name of the migration is 0005_initial . What are migrations 0001-0004 doing?

To fix your problem, you should first make sure you don't have duplicate migrations (for example, 0001_initial creates the tables, and then 0005_initial tries to do it again). You only need one initial migration for an app, next ones should only record changes to the schema.

Then, remove the tables from the database and create them again. Something like that might work:

./manage.py migrate [appname] --fake
./manage.py migrate [appname] zero
./manage.py migrate [appname]

If this fails (it will, if there are both existing and new tables in the migration) - then you have to drop tables by hand in the mysql console.

What's actually going on in your migrations? If you're getting this error then simply create_table is being called twice for blog_blog or blog_blog already existed in your database before the initial migration (perhaps, a leftover from a previous attempt at creating a blog that never got migrated to zero).

If the error is occurring before the initial migration completes, then it's most likely an artifact, and you can just remove it from your database manually and try again. If the initial migration succeeds but a later migration fails, then find where there's a duplicate create_table for blog_blog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM