mysql - 无缘无故在 Django 中出现 ""重复列名错误

标签 mysql django django-models

我在这里查看并通过 Google 查找,但没有发现任何内容似乎可以描述我所看到的内容。我正在使用 Django 1.7 和 Python 3.4。

预计到达时间:我正在使用 MySQL 5.6.17

我有以下模型(忽略了不相关的字段):

class Location(models.Model):
    location_type = models.CharField(max_length=5,
                                     choices=constants.LocationTypes.LOCATION_CHOICES
                                     )
    parent = models.ForeignKey("Location", blank=True, null=True, related_name='location_parent')
    room = models.ForeignKey("Location", blank=True, null=True, related_name='location_room')

除了评论之外,models.py 中的其他任何地方都没有“房间”的其他实例。

在我添加房间字段(同时将 related _name 添加到 parent 之前)它工作正常。现在,当我尝试运行迁移时,我得到以下信息:

File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 128, in execute return self.cursor.execute(query, args)
File "C:\Python34\lib\site-packages\pymysql\cursors.py", line 132, in execute result = self._query(query)
File "C:\Python34\lib\site-packages\pymysql\cursors.py", line 271, in _query conn.query(q)
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 726, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 861, in _read_query_result result.read()
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 1064, in read first_packet = self.connection._read_packet()
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 826, in _read_packet packet.check_error()
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 370, in check_error raise_mysql_exception(self._data)
File "C:\Python34\lib\site-packages\pymysql\err.py", line 116, in raise_mysql_exception _check_mysql_exception(errinfo)
File "C:\Python34\lib\site-packages\pymysql\err.py", line 112, in _check_mysql_exception raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1060, "Duplicate column name 'room_id'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/Phoenix/PycharmProjects/gamecon/manage.py", line 10, in <module> execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 338, in execute output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 63, in migrate self.apply_migration(migration, fake=fake)
  File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 97, in apply_migration migration.apply(project_state, schema_editor)
  File "C:\Python34\lib\site-packages\django\db\migrations\migration.py", line 107, in apply operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py", line 37, in database_forwards field,
  File "C:\Python34\lib\site-packages\django\db\backends\mysql\schema.py", line 42, in add_field super(DatabaseSchemaEditor, self).add_field(model, field)
  File "C:\Python34\lib\site-packages\django\db\backends\schema.py", line 390, in add_field self.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\schema.py", line 99, in execute cursor.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute return self.cursor.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python34\lib\site-packages\django\utils\six.py", line 549, in reraise raise value.with_traceback(tb)
  File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute return self.cursor.execute(sql, params)
  File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 128, in execute return self.cursor.execute(query, args)
  File "C:\Python34\lib\site-packages\pymysql\cursors.py", line 132, in execute result = self._query(query)
  File "C:\Python34\lib\site-packages\pymysql\cursors.py", line 271, in _query conn.query(q)
  File "C:\Python34\lib\site-packages\pymysql\connections.py", line 726, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Python34\lib\site-packages\pymysql\connections.py", line 861, in _read_query_result result.read()
  File "C:\Python34\lib\site-packages\pymysql\connections.py", line 1064, in read first_packet = self.connection._read_packet()
  File "C:\Python34\lib\site-packages\pymysql\connections.py", line 826, in _read_packet packet.check_error()
  File "C:\Python34\lib\site-packages\pymysql\connections.py", line 370, in check_error raise_mysql_exception(self._data)
  File "C:\Python34\lib\site-packages\pymysql\err.py", line 116, in raise_mysql_exception  _check_mysql_exception(errinfo)
  File "C:\Python34\lib\site-packages\pymysql\err.py", line 112, in _check_mysql_exception raise InternalError(errno, errorvalue)
django.db.utils.InternalError: (1060, "Duplicate column name 'room_id'")

有人可以告诉我我在这里缺少什么吗?

最佳答案

Made answer from comment

如果你在模型本身上做 ForeignKey 你应该这样做

models.ForeignKey("self", blank=True, null=True, related_name='location_parent')

关于mysql - 无缘无故在 Django 中出现 ""重复列名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27518346/

相关文章:

python - 对 'restaurants_list' 进行反向操作,未找到参数 '()' 和关键字参数 '{}'。尝试了 0 个模式 : []

php - 使用php加载带有选定选项的选择框

php - 在 Laravel 4 中搜索和过滤/精炼数据库结果

python - 在 django 中重新定义 home

django 测试 response.request.user.is_authenticated() 在注销后返回 True

python - 在指定位置提供 django 应用程序 (NGINX)

python - 我如何执行 django 模型验证以排除

mysql - 如果其他表没有相关数据,则更新多个表

mysql - 在 xampp 中复制整个 mysql 的最简单方法是什么?

html - 将两个不同样式的页面合并为一个