Django:迁移表“forum_user”已经存在
我更改了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么 django South 尝试创建的表已经存在,并且与数据库的状态不匹配。
如果这是您第一次迁移,请记住,在进行 schemamigration 更改之前,您必须通过
schemamigration myapp --initial
和migrate app --fake
设置初始状态> 将数据库与南数据库状态相匹配。manage.py Convert_to_south myapp
也可以作为一种便捷方法执行上述操作。要开始使用 South...
更新
注意 django 1.7+ 附带迁移和南方不再使用。
只有两个命令值得注意..
与 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
andmigrate 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...
Update
Note django 1.7+ ships with migrations and south is no longer in use.
There are only two commands worth noting..
Written by the same author as South, crowd funded. Go django.
我刚刚在本地修复了重复的表问题,并想记录我的工作流程以帮助其他人。
成功的关键是在添加新模型之前创建
--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:schemamigration --auto
was adding a table/model again and caused "already exists error".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).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...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.