Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#23065 closed Bug (fixed)

Migration Error when removing unique constraint

Reported by: anonymous Owned by: nobody
Component: Migrations Version: 1.7
Severity: Release blocker Keywords: migrations
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I try to make a migration which simple removes "unique = True" from a field. and get the error below.

Note this is nothing to do with Django's standard auth module, but a custom implementation, as far as I can see that should have nothing to do with it.

This is a private project so I can't give many more details than this. I'm using Postgres and the current 1.7 rc.

python manage.py makemigrations
Migrations for 'Auth':
  0003_auto_20140721_1413.py:
    - Alter field name on permission
(env)samuel || python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: bootstrapform, django_jinja, admin_tools, theming, dashboard
  Apply all migrations: Schedule, admin, sessions, auth, contenttypes, Accounts, Auth, CRM
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying Auth.0003_auto_20140721_1413...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 131, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 506, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 525, in _alter_field
    "name": constraint_name,
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 98, in execute
    cursor.execute(sql, params)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/path/to/project/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: constraint "auth_permission_name_key" of relation "Auth_permission" does not exist

Change History (10)

comment:1 Changed 6 years ago by anonymous

Oh, i can probably say the model looked liek this,

class Permission(models.Model):
    sys_name = models.CharField(_('System Name'), unique = True, max_length = 20)
    name = models.CharField(_('Name'), unique = True, max_length = 50)

    def __unicode__(self):
        return self.name

I wanted to remove the unique constraint from name.

comment:2 Changed 6 years ago by Tim Graham

I couldn't reproduce this (adding and removing a unique constraint. Does a unique constraint exist in the database? Maybe it has a different name? More details with steps to reproduce would be helpful.

comment:3 Changed 6 years ago by Tim Graham

Resolution: worksforme
Status: newclosed

comment:4 Changed 5 years ago by Sergey Fedoseev

Resolution: worksforme
Status: closednew
Triage Stage: UnreviewedAccepted

steps to reproduce postgres should be used as backend (maybe others, but not sqlite):

  • add Auth app with model as shown above
  • syncdb using django 1.6
  • upgrade to django 1.7
  • makemigrations Auth
  • migrate --fake
  • remove any unique constraint
  • makemigrations Auth
  • migrate

comment:5 Changed 5 years ago by Sergey Fedoseev

Has patch: set

comment:6 Changed 5 years ago by Sergey Fedoseev

Version: 1.7-rc-11.7

comment:7 Changed 5 years ago by Tim Graham

Needs tests: set
Severity: NormalRelease blocker

Regression tests are also required.

comment:8 Changed 5 years ago by Sergey Fedoseev

Needs tests: unset

tests and release notes entry added in the pull request

comment:9 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 83cd18633fb13924d8a50a1cd4ed21cb9552a635:

Fixed #23065 -- Quoted constraint names in SQL generated by migrations.

comment:10 Changed 5 years ago by Tim Graham <timograham@…>

In 58d7b84d53832a056128192c335127f10e082f2c:

[1.7.x] Fixed #23065 -- Quoted constraint names in SQL generated by migrations.

Backport of 83cd18633f from master

Note: See TracTickets for help on using tickets.
Back to Top