#2188 closed defect (fixed)
[patch] Older versions of MySQL cannot handle varchars > 255 characters in length.
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The problem
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.
Possible solutions
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
text
data type (instead ofvarchar
) whenmaxlength
> 255.
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 varchar
s and text
s differently? Also, since text
does not have a length specified, MySQL would not perform length validation when inserting or updating data.
What now?
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.
Attachments (2)
Change History (6)
by , 18 years ago
Attachment: | Option 2.diff added |
---|
comment:2 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Triggers an error when the model is validated.