迁移时重复的列名'model_id'django mysql错误

Posted

技术标签:

【中文标题】迁移时重复的列名\'model_id\'django mysql错误【英文标题】:Duplicate column name 'model_id' django mysql error on migrate迁移时重复的列名'model_id'django mysql错误 【发布时间】:2017-01-03 02:18:01 【问题描述】:

我在我的 VPS 上使用 Django 和 mysql。每当我运行python manage.py migrate 时,我都会收到以下错误。但是在我的开发服务器上,我使用 sqlite,并且迁移工作正常。

Running migrations:
  Rendering model states... DONE
  Applying bloupergroups.0002_auto_20160826_1138...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 396, in add_field
    self.execute(sql, params)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/ashish/Env/bloup/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
django.db.utils.OperationalError: (1060, "Duplicate column name 'blouper_id'")

我不明白这里有什么问题。

【问题讨论】:

请展示您的模型。 所有型号?或者任何特定的? 【参考方案1】:

Django 可能正在尝试应用已应用的迁移。

尝试运行python manage.py migrate --fake-initial

来自文档:

--假初始 如果迁移中所有 CreateModel 操作创建的所有模型名称的所有数据库表都已存在,则允许 Django 跳过应用程序的初始迁移。此选项适用于首次对已使用迁移的数据库运行迁移时使用。但是,此选项不会检查匹配表名之外的匹配数据库架构,因此只有在您确信现有架构与初始迁移中记录的架构匹配时才能安全使用。

【讨论】:

` 运行迁移:渲染模型状态... DONE Applying bloupergroups.0002_auto_20160826_1138... FAKED Applying feedback.0001_initial... OK ` 这是输出

以上是关于迁移时重复的列名'model_id'django mysql错误的主要内容,如果未能解决你的问题,请参考以下文章

Django在运行服务器和进行迁移时出错:[重复]

Django Migrations:使用 makemigrations 创建相同的迁移

Django迁移在Heroku上无法正常工作[重复]

如何取消应用 django 1.7 中的第一次迁移 [重复]

django数据库的表已迁移的不能重新迁移的解决办法

从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段