本文介绍了迁移Django数据库时列名重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迁移数据库:

I am trying to migrate my database:

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

当我尝试运行 python manage.py migration 时,出现了上面显示的错误.有人可以帮我解决这些问题吗?

When I try to run python manage.py migrate I got errors shown above. Can anybody help me fix them?

推荐答案

我想告诉你我的情况.仅当您有耐心并且确定要进行更改时,此方法才有效.Django 2.1

I want to tell you my case. This only works if you be patient and you are sure about the changes.Django 2.1

{%为崩溃%的崩溃}

{% for crash in crashes %}

  1. python manage.py makemigrations

  1. python manage.py makemigrations

找到冲突文件(在问题目录中.0002_auto_20170219_2146.py或其他内容)

Locate the conflict file (In the question catalog.0002_auto_20170219_2146.py or something)

暂时删除(复制到其他文件或不关闭文件)数据库中已经存在的列.(这是您必须要有耐心的:/).

Remove (copy in other file or don't close the file) temporally the columns that already exists into the database . (This is what you must be patient :/ ).

{%endfor%}

{% endfor %}

  1. python manage.py migration

  1. python manage.py migrate

撤消文件更改.

仅此而已,对我有用.道德:如果您发现与迁移存在冲突,则应将其修复,不要删除所有文件.

That's all, works for me.Moral: if you found an conflict with migrate, you should fix it, dont remove all files.

这篇关于迁移Django数据库时列名重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:39
查看更多