Opened 14 years ago
Closed 14 years ago
#14651 closed (invalid)
ignored field index creation for fields with unique=True and db_index=True
Reported by: | jordi | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If I have a field in my model like;
field = models.CharField(max_length=4, unique=True, db_index=True)
function sql_indexes_for_field from db/backends/creation.py will silently ignore creation the index for that field.
I think it show be created or, at least, give a message that it will not be created and some reason.
To create an index for such a field I have to remove "unique=True", create
the index whith "python manage.py sqlindexes <app>" and after add again "unique=True", so django admin captures
non unique entries. I am using sqlite.
Change History (4)
comment:1 by , 14 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 14 years ago
In the case
field = models.CharField?(max_length=4, unique=True, db_index=True)
I just need a quick access to this field, and it must be a unique. If the field is part of an other index I think it not the matter.
In the code, creation of indexes with (unique=True and db_index=True) are just ignored:
http://code.djangoproject.com/browser/django/trunk/django/db/backends/creation.py#L260
If there is a reason, the user should be warned and it should be stated in the documentation http://docs.djangoproject.com/en/1.2/ref/models/fields/#common-model-field-options
That's all, thanks.
comment:3 by , 14 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
comment:4 by , 14 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
I'm in agreement with Ramiro. A UNIQUE column *IS* indexed. You don't need to create an additional index. That's why it's optimized out of the table creation code.
Perhaps this is beacuase of this? (from http://www.sqlite.org/lang_createtable.html)
INTEGER PRIMARY KEY columns aside, both UNIQUE and PRIMARY KEY constraints are implemented by creating an index in the database (in the same way as a "CREATE UNIQUE INDEX" statement would). Such an index is used like any other index in the database to optimize queries. As a result, there often no advantage (but significant overhead) in creating an index on a set of columns that are already collectively subject to a UNIQUE or PRIMARY KEY constraint.
Closing the ticket, please reopen if I've misunderstood things.