Opened 13 years ago
Closed 13 years ago
#16460 closed New feature (invalid)
Add support for Index Length for MySQL-backed Models
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | umbrae@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
On occasion, it is useful to be able to specific a key length different than the field length for a field - this is particularly true with MySQL, whose index sizes are limited to 767 bytes in InnoDB.
For example, I have a simple field like the following:
url = models.CharField(max_length=767, db_index=True)
Where all fields are UTF-8 characters, and hence 3 bytes each. This will issue a warning when I try to syncdb (and will full-on fail in south, as south bombs out on warnings):
_mysql_exceptions.Warning: Specified key was too long; max key length is 767 bytes
What I'd like to be able to do is the following:
url = models.CharField(max_length=767, db_index=True, index_length=255)
Which will then change the CREATE INDEX sql to look like:
CREATE INDEX "some_table_a4b49ab" ON "some_table" ("url"(255));
This should no longer issue a warning in MySQL.
Change History (3)
comment:1 by , 13 years ago
comment:2 by , 13 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
The idea makes sense, and wasn't reported before. I'm not sure if we want to add this in Django, though.
comment:3 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Adding database specific attributes to Field classes isn't an option. We already allow initial SQL as well as emitting a syncdb signal that can be used for just this purpose: custom creation SQL insertion. That is where you would put code to create the appropriate index.
(Addendum: I know that, in general, such long indexes are discouraged - they are sometimes necessary however.)