迁移Django数据库时重复列名

时间:2017-02-19 19:58:15

标签: 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 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 migrate时,我收到上面显示的错误。任何人都可以帮我修理它们吗?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

这是我也面临的一个简单问题。 只是一个console.log(window.getComputedStyle(body, null).getPropertyValue("background")); 命令问题没什么大不了的。通常,当您创建新模型或新表时,您将重新运行旧迁移以及新创建的迁移。有时候,我想让Django让我感到困惑。

我发现的最简单的解决方案是

  1. 首先清空您的回收站。
  2. 然后转到您的一个或多个迁移文件夹(如果您拥有多个应用程序)
  3. 临时删除(将所有迁移文件(例如migrate 0001_initial.py等发送给回收站。

  4. 然后重新运行命令0002_auto_20170621_1006python manage.py makemigrations

*步骤3:是临时删除。在这里您还可以返回到回收站,只需单击一下即可轻松将其还原。 (所以del是安全的)

答案 2 :(得分:0)

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

{%为崩溃的%%}

  1. python manage.py makemigrations

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

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

{%endfor%}

  1. python manage.py migration

  2. 撤消文件更改。

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