|
0
|
1 |
from django.db.backends import BaseDatabaseValidation |
|
|
2 |
|
|
|
3 |
class DatabaseValidation(BaseDatabaseValidation): |
|
|
4 |
def validate_field(self, errors, opts, f): |
|
|
5 |
""" |
|
|
6 |
There are some field length restrictions for MySQL: |
|
|
7 |
|
|
|
8 |
- Prior to version 5.0.3, character fields could not exceed 255 |
|
|
9 |
characters in length. |
|
|
10 |
- No character (varchar) fields can have a length exceeding 255 |
|
|
11 |
characters if they have a unique index on them. |
|
|
12 |
""" |
|
|
13 |
from django.db import models |
|
|
14 |
from django.db import connection |
|
|
15 |
db_version = connection.get_server_version() |
|
|
16 |
varchar_fields = (models.CharField, models.CommaSeparatedIntegerField, |
|
|
17 |
models.SlugField) |
|
|
18 |
if isinstance(f, varchar_fields) and f.max_length > 255: |
|
|
19 |
if db_version < (5, 0, 3): |
|
|
20 |
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %(version)s).' |
|
|
21 |
elif f.unique == True: |
|
|
22 |
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".' |
|
|
23 |
else: |
|
|
24 |
msg = None |
|
|
25 |
|
|
|
26 |
if msg: |
|
|
27 |
errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])}) |
|
|
28 |
|