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:
class Btilog(models.Model):
md5hash = models.TextField(db_index=False)
[...]
And in migration also force add db_field=False
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('logger', '0014_btilog_id'),
]
operations = [
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=
Index "public.logger_btilog_md5hash_6454d7bb20588b61_like"
Column | Type | Definition
---------+------+------------
md5hash | text | md5hash
btree, for table "public.logger_btilog"
: -, = ( ) where md5hash, () hash , btree ( django)