Django dump : models don't validate
When I try to dump the data of my model in Django with this instruction :
python manage.py dumpdata app> temp_data.json
It gives the following error :
Error: One or more models did not validate:
asset.authpermission: "codename": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype开发者_高级运维: "app_label": CharField cannot have a "max_length" greater than 255 when using "unique=True".
asset.djangocontenttype: "model": CharField cannot have a "max_length" greater than 255 when using "unique=True".
The thing is that these tables are auto-generated by django. Plus I just checked in the database (mysql) and the fields are varchar(100).
What's wrong ?
Those tables are generated by a manage.py inspectdb
right? Then you don't need to include django's own models into the actual generated models. Just remove any model starting with auth
, django
, admin
and site
.
Just include the corresponding contrib apps to the INSTALLED_APPS setting and voila, no more errors.
Django is attempting to validate your schema against what the database backend will allow before it lets you dump. The issue is this:
CharField cannot have a "max_length" greater than 255 when using "unique=True".
The problem you're experiencing is this: CharField
translates to VARCHAR
in SQL - if you have max_length
that translates to VARCHAR(X)
where X
is whatever you set the maximum length to.
MySQL, for tables that aren't MyISAM, won't index (hash) for searching any data longer than 255
characters in a CharField
. This also rules out TextField
which translates to SQL TEXT
.
The MySQL docs on indexing are fairly comprehensive. The CREATE INDEX
documentation gets you to the heart of the issue:
FULLTEXT indexes are supported only for MyISAM tables and can include only CHAR, VARCHAR, and TEXT columns. Indexing always happens over the entire column; column prefix indexing is not supported and any prefix length is ignored if specified. See Section 11.9, “Full-Text Search Functions”, for details of operation.
In other words, unless you're using MyISAM as your storage format, you can't do this.
There is an argument either way as to whether this is a design fault or not; on the one hand, you could argue that really, if you need to index such a huge bucket of data as a full pile of text, you really need to think about that more carefully. On the other hand, you could argue that 255 is an arbitrary choice for a limit. Why 255 and not 300? Or 200? I d
I've just hit upon this issue myself. The solution is to ask whether unique=True
is actually required, then decide if max_length
really does need to be that long. In my case we were storing sha512 output, which is 128 chars long, so I just resized the fields appropriately.
However, if you do need unique long data, you can enforce this yourself by overriding a models save()
method. What you actually want to do is override the full_clean
method, which is called by django.forms.*
to validate your model's data against the schema, then have an overridden save
method call that. That way, the uniqueness constraint occurs whether you save directly or call is_valid()
on a form.
The other option is to switch to MyISAM, or use postgres.
精彩评论