获取“”重复列名称“ Django中的错误没有明显的原因

编程入门 行业动态 更新时间:2024-10-28 00:18:22
本文介绍了获取“”重复列名称“ Django中的错误没有明显的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在这里看过谷歌,并没有发现任何似乎描述我所看到的东西。我使用Django 1.7与Python 3.4。

I've looked here and through Google, and found nothing that seems to describe what I'm seeing. I'm using Django 1.7 with Python 3.4.

ETA:我使用MySQL 5.6.17

ETA: I'm using MySQL 5.6.17

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

I have the following model (unrelated fields left out):

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中没有其他任何room的实例。

There are no other instances of "room" anywhere else in models.py except for a comment.

它工作正常,直到我添加房间字段(同时将相关_name 添加到 parent )。现在,当我尝试运行迁移时,我得到以下内容:

It worked fine until I added the room field (adding the related _name to parent at the same time). Now when I try to run the migration, I get the following:

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

Can someone please tell me what I'm missing here?

推荐答案

/ p>

Made answer from comment

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

if you are doing ForeignKey on the model itself you should do it like this

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

更多推荐

获取“”重复列名称“ Django中的错误没有明显的原因

本文发布于:2023-11-24 01:22:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   名称   原因   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!