Issue #77 new

Cannot change column with index

qurben
created an issue

When running a migration altering a field in a model from null to not null, MSSQL refuses to change the column while there is an index attached to it.

The following error is returned when making a (foreign key) field not null.

I am using Django 1.7.8 and django-mssql 1.7

File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "site-packages\django\core\management\commands\migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "site-packages\django\db\migrations\executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "site-packages\django\db\migrations\executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "site-packages\django\db\migrations\migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "site-packages\django\db\migrations\operations\fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "site-packages\django\db\backends\schema.py", line 470, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "site-packages\sqlserver_ado\schema.py", line 316, in _alter_field
    params,
  File "site-packages\django\db\backends\schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "site-packages\sqlserver_ado\dbapi.py", line 632, in execute
    self._execute_command()
  File "site-packages\sqlserver_ado\dbapi.py", line 548, in _execute_command
    self._raiseCursorError(klass, _message)
  File "site-packages\sqlserver_ado\dbapi.py", line 478, in _raiseCursorError
    eh(self.connection, self, errorclass, errorvalue)
  File "site-packages\sqlserver_ado\dbapi.py", line 100, in standardErrorHandler
    raise errorclass(errorvalue)
django.db.utils.DatabaseError: (-2147352567, 'Exception occurred.', (0, u'Microsoft SQL Server Native Client 11.0', u"The index 'core_resource_e274a5da' is dependent on column 'location_id'.", None, 0, -2147217900), None)
Command:
ALTER TABLE [core_resource] ALTER COLUMN [location_id] nvarchar(80) NOT NULL
Parameters:
[]

Comments (2)

  1. Ernst Haagsman

    This prevents me from running migrate to initially create the database, because migration 'auth.0008' from django.contrib.auth alters the length of the username column, which has two constraints applied to it.

    Django 1.10 SQL Server 2016 Express Django-mssql 1.8

    django.db.utils.DatabaseError: (-2147352567, 'Exception occurred.', (0, u'Microsoft SQL Server Native Client 11.0', u"The object 'UQ__auth_use__F3DBC5721B12F07C' is dependent on column 'username'.", None, 0, -2147217900), None)
    Command:
    ALTER TABLE [auth_user] ALTER COLUMN [username] nvarchar(150) NOT NULL
    
  2. Log in to comment