﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26805	Dropping unique from CharField does not drop PostgreSQL _like index	Jon Dufresne	nobody	"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 [https://github.com/django/django/blob/ca77b509059831b055a3b735ff77e042f8e1c0eb/tests/schema/tests.py#L1813-L1841 similar tests] in `schema/tests.py`:

{{{#!python
    @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']
+ []
}}}"	Bug	closed	Migrations	dev	Normal	invalid			Unreviewed	0	0	0	0	0	0
