[patch] Older versions of MySQL cannot handle varchars > 255 characters in length.
|Reported by:||Owned by:||Adrian Holovaty|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Versions of MySQL below 5.0.3 only support
varchar fields with a maximum length of 255 characters (see the MySQL docs for details). When Django is generating the create SQL for a CharField, CommaSeparatedIntegerField or SlugField it uses the
maxlength attribute to specify the
varchar length. This clearly has the potential to produce SQL which is not accepted by older (yet still very popular and prevalent) versions of MySQL.
As I see it, there are three options to address this:
- document this as a known issue and leave it up to developers to deal with the problem manually;
- detect the problem when validating the model and display an error to the user telling them they can't have a
maxlength> 255; or
- detect the problem when creating the database and use the
textdata type (instead of
Pros and cons of the solutions
I think option one would be the minimum, but really isn't that helpful and doesn't deal with the problem itself. I don't think this is the way to go.
Option two would stop the problem from occurring, but is obviously restrictive and wouldn't allow you the freedom to easily switch between database backends.
The use of the
text data type in option three pretty much removes any restrictions on the
maxlength of a field, but may have undesired performance issues -- does MySQL treat
texts differently? Also, since
text does not have a length specified, MySQL would not perform length validation when inserting or updating data.
I attach patches for options two and three, and will leave it to your judgement as to which would be more appropriate and in keeping with the Django ethos.