Opened 8 years ago

Closed 8 years ago

#26805 closed Bug (invalid)

Dropping unique from CharField does not drop PostgreSQL _like index

Reported by: Jon Dufresne Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a CharField is created with unique=True and without db_index=True the PostgreSQL _like index is added to the field. (Good)

Later, if unique is removed from the field. The unique constraint is removed during migrations, but the corresponding _like index is not cleaned up.

The following unit test demonstrates this behavior. It was adapted from similar tests in schema/tests.py:

    @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
    def test_alter_field_drop_unique_from_charfield(self):
        # Create the table and verify initial indexes.
        with connection.schema_editor() as editor:
            editor.create_model(Tag)
        self.assertEqual(
            self.get_constraints_for_column(Tag, 'slug'),
            ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
        )
        # Alter to drop unique
        old_field = Tag._meta.get_field('slug')
        new_field = SlugField()
        new_field.set_attributes_from_name('slug')
        with connection.schema_editor() as editor:
            editor.alter_field(Tag, old_field, new_field, strict=True)
        self.assertEqual(self.get_constraints_for_column(Tag, 'slug'), [])

Running this produces the following failure:

======================================================================
FAIL: test_alter_field_drop_unique_from_charfield (schema.tests.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jon/devel/django/tests/schema/tests.py", line 1858, in test_alter_field_drop_unique_from_charfield
    self.assertEqual(self.get_constraints_for_column(Tag, 'slug'), [])
AssertionError: Lists differ: ['schema_tag_slug_2c418ba3_like'] != []

First list contains 1 additional elements.
First extra element 0:
schema_tag_slug_2c418ba3_like

- ['schema_tag_slug_2c418ba3_like']
+ []

Change History (1)

comment:1 by Jon Dufresne, 8 years ago

Resolution: invalid
Status: newclosed

My mistake. This is invalid. The SlugField has an implied db_index which is why the _like index remains.

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