使用 South (django) 和 MySQL 进行架构迁移

发布于 2024-12-09 20:45:34 字数 2984 浏览 1 评论 0原文

我刚刚完成了 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 会跳过这个错误,但它不会改变什么?这里有什么问题以及如何解决它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖凝香 2024-12-16 20:45:34

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

manage.py migrate blog --fake

...或从 mysql 控制台手动删除表“blog_blog”;)

Are you sure adding --fake doesnt change anything ?

manage.py migrate blog --fake

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

小傻瓜 2024-12-16 20:45:34

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

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

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

然后,从数据库中删除表并再次创建它们。类似的方法可能会起作用:

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

如果失败(如果迁移中同时存在现有表和新表,则会失败)-那么您必须在 mysql 控制台中手动删除表。

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.

苦妄 2024-12-16 20:45:34

您的迁移中实际发生了什么?如果您收到此错误,则只需为 blog_blog 调用 create_table 两次,或者在初始迁移之前数据库中已存在 blog_blog(可能是,这是之前尝试创建博客的剩余部分,但从未迁移到零)。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文