使用south(django)和MySQL进行模式迁移

时间:2011-10-13 19:59:04

标签: python mysql django django-south

我刚刚阅读了关于django的教程,我遵循了每一步,但每次尝试迁移时我都会收到此错误:

初始迁移有效,但尝试迁移它,我明白了:

  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")

我已经四处搜索并发现添加--fake会跳过此错误,但它不会改变一件事吗?这里有什么问题,如何解决?

3 个答案:

答案 0 :(得分:3)

你确定添加--fake不会改变任何东西吗?

manage.py migrate blog --fake

...或从mysql控制台手动删除表'博客博客';)

答案 1 :(得分:3)

问题是迁移尝试创建的表已存在于数据库中。它们必须是使用syncdb创建的(如果您执行syncdb并稍后开始使用应用程序的南),或者之前的迁移,则会获得此结果。

移植的名称是0005_initial是可疑的。什么是迁移0001-0004在做什么?

要解决您的问题,首先应确保没有重复迁移(例如,0001_initial创建表,然后0005_initial尝试再次执行此操作)。您只需要为应用程序进行一次初始迁移,接下来只应记录对模式的更改。

然后,从数据库中删除表并再次创建它们。这样的事情可能有用:

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

如果失败(如果迁移中存在现有表和新表的话),则必须在mysql控制台中手动删除表。

答案 2 :(得分:1)

您的迁移实际发生了什么?如果您收到此错误,那么在初始迁移之前,只有create_tableblog_blog已经在数据库中已经存在两次blog_blog被调用(可能是之前尝试创建的遗留物)一个从未被迁移到零的博客。)

如果错误在初始迁移完成之前发生,那么它很可能是一个工件,您可以手动将其从数据库中删除,然后重试。如果初始迁移成功但稍后的迁移失败,则找到create_table的重复blog_blog的位置。