How to remove varchar_pattern_ops index in django (1.8) migration?

When creating a model with a field models.varchar(...), an index is created varchar_pattern_ops.

This is a table generated in postgresql

              Table "public.logger_btilog"
      Column      |           Type           | Modifiers 
------------------+--------------------------+-----------
 md5hash          | text                     | 
 id               | integer                  | not null
Indexes:
    "logger_btilog_pkey" PRIMARY KEY, btree (id)
    "logger_btilog_md5hash_6454d7bb20588b61_like" btree (md5hash varchar_pattern_ops)

I want to remove this index varchar_pattern_opswhen migrating and add a hash index to this field.

I tried to do this:

# models.py
class Btilog(models.Model):
    md5hash = models.TextField(db_index=False)
    [...]

And in migration also force add db_field=False

# 0013_migration.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('logger', '0014_btilog_id'),
    ]

    operations = [
        # this should remove all indexes for md5hash, but it does not work
        migrations.AlterField(
            model_name='btilog',
            name='md5hash',
            field=models.TextField(null=True, blank=True, db_index=False),
        ),
        migrations.RunSQL(
            "create index logger_btilog_md5hash_hashindex on logger_btilog using hash(md5hash);",
            "drop index logger_btilog_md5hash_hashindex;"
        ),
]

After starting the migration, these are the indexes in the database

                              relation                              |  size   
--------------------------------------------------------------------+---------
 public.logger_btilog                                               | 7185 MB
 public.logger_btilog_md5hash_6454d7bb20588b61_like                 | 1442 MB
 public.logger_btilog_md5hash_hashindex                             | 1024 MB
 public.logger_btilog_pkey                                          | 548 MB

Please note that public.logger_btilog_md5hash_6454d7bb20588b61_likeis the index I want to remove. This index is added automatically by django, see this

Additional information about this index

vtfx=# \d logger_btilog_md5hash_6454d7bb20588b61_like
Index "public.logger_btilog_md5hash_6454d7bb20588b61_like"
 Column  | Type | Definition 
---------+------+------------
 md5hash | text | md5hash
btree, for table "public.logger_btilog"

: -, = ( ) where md5hash, () hash , btree ( django)

+4
1

, https://docs.djangoproject.com/en/1.8/_modules/django/db/backends/base/schema/#BaseDatabaseSchemaEditor.alter_field

RunPython varchar_pattern_ops SchemaEditor

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


import re
def drop_md5hash_varchar_pattern_ops_index(apps, schemaEditor):
    # code based on https://docs.djangoproject.com/en/1.8/_modules/django/db/backends/base/schema/#BaseDatabaseSchemaEditor.alter_field
    model = apps.get_model("logger", "Btilog")
    index_names = schemaEditor._constraint_names(model, index=True)
    for index_name in index_names:
        if re.search('logger_btilog_md5hash_.+_like', index_name):
            print 'dropping index {}'.format(index_name)
            schemaEditor.execute(schemaEditor._delete_constraint_sql(schemaEditor.sql_delete_index, model, index_name))


class Migration(migrations.Migration):
    dependencies = [
        ('logger', '0012_auto_20150529_1745'),
    ]

    operations = [
        # Remove the annoying index using a hack
        migrations.RunPython(
            drop_md5hash_varchar_pattern_ops_index
        ),
    ]
+7

Source: https://habr.com/ru/post/1608912/


All Articles