Django:迁移表“forum_user”已经存在

发布于 2025-01-07 13:00:30 字数 239 浏览 1 评论 0原文

我更改了 Django 模型,并使用 Django schemamigration 来更新数据库。但是,当我执行 python manager.py migrate app 时,它会抛出以下错误消息:

_mysql_exceptions.OperationalError: (1050, "Table 'forum_user' already exists")

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

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

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

发布评论

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

评论(2

淡墨 2025-01-14 13:00:30

那么 django South 尝试创建的表已经存在,并且与数据库的状态不匹配。

如果这是您第一次迁移,请记住,在进行 schemamigration 更改之前,您必须通过 schemamigration myapp --initialmigrate app --fake 设置初始状态> 将数据库与南数据库状态相匹配。

manage.py Convert_to_south myapp 也可以作为一种便捷方法执行上述操作。

要开始使用 South...

  1. 请确保您的 django 表与当前数据库表完全匹配 - 如果您计划添加或删除列,请将其注释掉。
  2. 运行 python manage.py schemamigration myapp --initial
  3. 运行 python manage.py migrate myapp --fake
  4. 对您的 django 模型进行更改
  5. 运行 python manage.py schemamigration myapp --auto
  6. 运行 python manage.py migrate myapp

更新

注意 django 1.7+ 附带迁移和南方不再使用。

只有两个命令值得注意..

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

与 South 同一作者编写,众筹。去姜戈吧。

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.

爱你不解释 2025-01-14 13:00:30

我刚刚在本地修复了重复的表问题,并想记录我的工作流程以帮助其他人。

成功的关键是在添加新模型之前创建 --empty 迁移。流程:

  • 合并到另一个人的工作中,排除了我本地模型的信息。
  • 正常的 schemamigration --auto 再次添加表/模型并导致“已存在错误”。
  • 通过注释掉新模型并通过 clear 运行空迁移来解决; python manage.py schemamigration --empty APPNAME MIGRATION_FILE_NAME。这创建了模型状态的“声明”,没有前进/后退命令。 100% 确定模型(python 文件)和数据库的当前状态是同步的!此最新迁移将用于创建差异以正确迁移(下一步)。
  • 取消注释新模型并运行 clear; python manage.py schemamigration APPNAME --auto 创建真实且所需的差异关闭(使用刚刚创建的 --empty 迁移)。新迁移将具有适合您的新模型的前进/后退命令。回顾...
  • clear; 结束python manage.py migrate

吸取的教训是 --auto 查看最后一个 APP+migration 文件以创建向前/向后差异。如果上次迁移的字典中没有您在数据库中拥有的模型,则会再次创建该模型,从而导致“已存在”错误。将字典视为 Django 和数据库之间的契约,声明“这就是一切应该是什么样子”。迁移可以包含创建重复表的命令,并且仅在“migrate”命令期间公开。

上述信息应该可以解决问题。提出的部分目的是为了帮助人们,也为了防止我做了愚蠢的事情而进行审查。

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.

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