[Django] #26805: Dropping unique from CharField does not drop PostgreSQL _like index

6 views
Skip to first unread message

Django

unread,
Jun 26, 2016, 12:02:50 PM6/26/16
to django-...@googlegroups.com
#26805: Dropping unique from CharField does not drop PostgreSQL _like index
----------------------------+--------------------
Reporter: jdufresne | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
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']
+ []
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26805>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 26, 2016, 2:36:02 PM6/26/16
to django-...@googlegroups.com
#26805: Dropping unique from CharField does not drop PostgreSQL _like index
----------------------------+--------------------------------------
Reporter: jdufresne | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by jdufresne):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/26805#comment:1>

Reply all
Reply to author
Forward
0 new messages