Code

Opened 3 years ago

Closed 3 years ago

#16460 closed New feature (invalid)

Add support for Index Length for MySQL-backed Models

Reported by: umbrae@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
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.

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by umbrae@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

(Addendum: I know that, in general, such long indexes are discouraged - they are sometimes necessary however.)

comment:2 Changed 3 years ago by aaugustin

  • Triage Stage changed from Unreviewed to 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 Changed 3 years ago by mtredinnick

  • Resolution set to invalid
  • Status changed from new to 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.