Python 迁移Django数据库时列名重复

Python 迁移Django数据库时列名重复,python,django,Python,Django,我正在尝试迁移我的数据库: E:\PhytonProgects\natarelke>python manage.py migrate System check identified some issues: WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default' HINT: MySQL's Strict Mode fixes many data i

我正在尝试迁移我的数据库:

E:\PhytonProgects\natarelke>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection       'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL,     such as data truncation upon insertion, by escalating warnings into errors. It   is strongly recommended
you activate it. See: 
https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, catalog, contenttypes, main, ordering,     registration, sessions, users
Running migrations:
Rendering model states... DONE
Applying catalog.0002_auto_20170219_2146...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)

File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line   367, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 356, in execute
output = self.handle(*args, **options)

File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
targets, plan, fake=fake, fake_initial=fake_initial

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 97, in migrate
state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 132, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 237, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
 File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
 File "C:\Python27\lib\site-packages\django\db\backends\mysql\schema.py", line 43, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
 File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 409, in add_field
self.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
cursor.execute(sql, params)
 File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1060, "Duplicate column name 'user_id'")
E:\PhytonProgects\natarelke>python manage.py迁移
系统检查发现了一些问题:
警告:
?:(mysql.W002)数据库连接“默认”未设置mysql严格模式
提示:MySQL的严格模式通过将警告升级为错误,修复了MySQL中的许多数据完整性问题,如插入时的数据截断。强烈建议您这样做
你激活它。见:
https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-sql模式
要执行的操作:
应用所有迁移:管理、身份验证、目录、contenttypes、主、排序、注册、会话、用户
运行迁移:
正在渲染模型状态。。。完成
正在应用catalog.0002_auto_20170219_2146…回溯(最近一次调用):
文件“manage.py”,第22行,在
从命令行(sys.argv)执行命令
文件“C:\Python27\lib\site packages\django\core\management\\ uuuu init\uuuu.py”,第367行,从命令行执行
utility.execute()
文件“C:\Python27\lib\site packages\django\core\management\\uuuu init\uuuuu.py”,第359行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Python27\lib\site packages\django\core\management\base.py”,第305行,运行于\u argv
self.execute(*args,**cmd_选项)
文件“C:\Python27\lib\site packages\django\core\management\base.py”,第356行,在execute中
输出=self.handle(*args,**选项)
文件“C:\Python27\lib\site packages\django\core\management\commands\migrate.py”,第202行,位于句柄中
目标,计划,假=假,假首字母=假首字母
文件“C:\Python27\lib\site packages\django\db\migrations\executor.py”,第97行,在migrate中
状态=自我。\迁移\所有\向前(计划、完整\计划、假=假、假首字母=假首字母)
文件“C:\Python27\lib\site packages\django\db\migrations\executor.py”,第132行,全部向前迁移
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“C:\Python27\lib\site packages\django\db\migrations\executor.py”,第237行,在apply\u migration中
state=migration.apply(状态,模式编辑器)
文件“C:\Python27\lib\site packages\django\db\migrations\migration.py”,第129行,在apply中
操作。数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)
数据库中第84行的文件“C:\Python27\lib\site packages\django\db\migrations\operations\fields.py”
领域
文件“C:\Python27\lib\site packages\django\db\backends\mysql\schema.py”,第43行,在add\u字段中
super(DatabaseSchemaEditor,self).add_字段(model,field)
文件“C:\Python27\lib\site packages\django\db\backends\base\schema.py”,第409行,在add\u字段中
self.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\base\schema.py”,执行中的第112行
cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\utils.py”,执行中的第79行
返回super(CursorDebugWrapper,self).execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\utils.py”,执行中的第64行
返回self.cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\utils.py”,第94行,在退出时__
6.重新播放(dj_exc_类型、dj_exc_值、回溯)
文件“C:\Python27\lib\site packages\django\db\backends\utils.py”,执行中的第64行
返回self.cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\mysql\base.py”,执行中的第112行
返回self.cursor.execute(查询,参数)
文件“C:\Python27\lib\site packages\MySQLdb\cursors.py”,第205行,在execute中
errorhandler(self、exc、value)
defaulterrorhandler中第36行的文件“C:\Python27\lib\site packages\MySQLdb\connections.py”
提高errorclass,errorvalue
django.db.utils.OperationalError:(1060,“重复的列名'user_id'”)

当我尝试运行
python manage.py migrate
时,出现了上面显示的错误。有人能帮我修复它们吗?

您的数据库或迁移或两者都有问题。如果这是一个新项目,删除数据库和所有迁移,并使用
makemigrations
重新创建它们,可能会让您摆脱麻烦


要保存数据,可以尝试还原到与数据库匹配的models.py,并使用dumpdata将数据导出到JSON文件。

这是我也面临的一个简单问题。 这没什么大不了的,只是
migrate
命令的问题。通常,在创建新模型或新表时,您会重新运行旧迁移和新创建的迁移。我想在这里,Django有时会把这件事弄糊涂

我找到的最简单的解决办法是

  • 首先清空你的回收站
  • 然后转到您的迁移文件夹(如果您有多个应用程序)
  • 临时删除(将所有迁移文件,如
    0001_initial.py
    0002_auto\u 20170621_1006
    等发送到回收站

  • 然后重新运行命令
    python manage.py makemigrations
    python manage.py migrate


  • *第3步:是临时删除。在这里,您还可以轻松返回回收站,只需单击一下即可恢复。(因此删除是安全的)

    我想告诉您我的情况。这只有在您耐心且确信更改的情况下才有效。 Django 2.1

    {%用于崩溃中的崩溃%}

  • python manage.py makemigrations

  • 找到冲突文件(在question catalog.0002_auto_20170219_2146.py或其他文件中)

  • 暂时删除(复制到其他文件或不关闭文件)数据库中已存在的列。(这是您必须遵守的:/)

  • {%endfor%}

  • python manage.py迁移

  • 撤消文件中的更改

  • 就这些,对我有用。
    寓意:如果您发现与“迁移”冲突,您应该修复它,不要删除所有文件。

    无需重复您自己的操作……错误为“重复列名'user\u id'”。您的数据库中已经有表了吗?是的,我有,但我没有