Prithviraj Mitra
Prithviraj Mitra

Reputation: 11842

Migrate command error in Django

I am getting a migrate command error --

django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'module_name' used in key specification without a key length")

The model is

class Coupling(models.Model):
    coupling_name = models.CharField(max_length=150, blank=False, default=True, unique=True)
    module_name = models.TextField(max_length=255)

There is a key length though.

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\virtualenvs\ciasenv\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:\virtualenvs\ciasenv\lib\site-packages\django\db\migrations\operations\fields.py", line 204, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 495, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 678, in _alter_field
    self.execute(self._create_unique_sql(model, [new_field.column]))
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
    cursor.execute(sql, params)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\virtualenvs\ciasenv\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\virtualenvs\ciasenv\lib\site-packages\MySQLdb\connections.py", line 292, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1061, "Duplicate key name 'accounts_coupling_coupling_name_175325e1_uniq'")

Any help is highly appreciated. Thanks in advance.

Upvotes: 0

Views: 685

Answers (1)

Alasdair
Alasdair

Reputation: 309089

The first error might be because you tried to set unique=True for the TextField. This is not possible for MySQL, as noted in the docs.

The second error is because you are trying to create an index that already exists. This could be because you already ran the migration and it failed part way through. MySQL does not run schema changes in a transaction, so changes aren't rolled back if they fail. You could try dropping the index manually, or, if this is a new project, it might be easier to drop the database and start again.

Upvotes: 2

Related Questions