Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ""Duplicate column name" error in Django for no apparent reason

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

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

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?

like image 241
Beverly Block Avatar asked Oct 31 '22 12:10

Beverly Block


1 Answers

Made answer from comment

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

like image 89
Sardorbek Imomaliev Avatar answered Nov 15 '22 05:11

Sardorbek Imomaliev