如何在Django(1.8)迁移中删除索引varchar_pattern_ops? [英] How to remove index varchar_pattern_ops in a django (1.8) migration?
问题描述
使用 models.varchar(...)
字段创建模型时,将创建 varchar_pattern_ops
索引.
When creating a model with a models.varchar(...)
field, a varchar_pattern_ops
index is being created.
这是在postgresql中生成的表
This is the 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)
我想在迁移中删除该 varchar_pattern_ops
索引,并在该字段中添加一个哈希索引.
I want to remove that varchar_pattern_ops
index in a migration, and add a hash index in that field.
我尝试这样做:
# models.py
class Btilog(models.Model):
md5hash = models.TextField(db_index=False)
[...]
在迁移过程中,还强制添加 db_field = False
And in migration also force adding 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 running the migrations, this 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
请注意, public.logger_btilog_md5hash_6454d7bb20588b61_like
是我要删除的索引.django会自动添加此索引,请参见此
Note that public.logger_btilog_md5hash_6454d7bb20588b61_like
is the index I want to delete. This index is being added automatically by django, see this
有关该索引的更多信息
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的默认值)
Footnote: I'm not confused about the usage of a hash index, I only want to do =
(strictrly equal) where
searches in md5hash
field, then (casually) a hash
index would be the fastest and will occupy less space than a btree
index (django's default)
推荐答案
回答更新通知:
-
django<1.11:使用此答案
django < 1.11: Use this answer
django> = 1.11:使用@Cesar Canassa的答案
django >= 1.11: Use @Cesar Canassa's answer
Ok, I found some info here https://docs.djangoproject.com/en/1.8/_modules/django/db/backends/base/schema/#BaseDatabaseSchemaEditor.alter_field
并进行了手动 RunPython
迁移,以使用 SchemaEditor
And made a manual RunPython
migration to delete the varchar_pattern_ops
index using the 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
),
]
这篇关于如何在Django(1.8)迁移中删除索引varchar_pattern_ops?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!