nedeľa 13. septembra 2009

Long model names in Django

IMPORTANT UPDATE: Probably better solution, than extending varchar sizes as described below, is not allow long verbose names to exceed particular varchar size. It does not mean, that you can't have long verbose names! You only need to do small trick: when adding new models, do not populate verbose names! You can do it after first syncdb, but default string values created by first syncdb in auth_permission and django_content_type won't be replaced! Do following not till this is not sufficient.

UPDATE: Attribute codename is probably not an issue.


There is a limitation in Django - when using long model names (class names, attribute names, multiple inheritations, long verbose names, ...), you may hit a snag, when running python manage.py syncdb:

psycopg2.DataError: value too long for type character varying(50)
or
psycopg2.DataError: value too long for type character varying(100)

The root cause is in two automatically generated fields in auth_permission table: name (varchar(50) by default) and codename (varchar(100) by default).

This issue is described for example here:
http://groups.google.com/group/django-users/browse_thread/thread/964fdef3e374e35c
http://www.mail-archive.com/django-users@googlegroups.com/msg73888.html

This behaviour of Django probably won't be changed, due to potential backwards incompatibility, so if your project is generating long model names and you will hit error messages above, do not suffer, and extend varchar maximum length for name and codename in table auth_permission.

I've changed both to varchar(200) by pgAdmin and after following syncdb error message disappeared (for now ;), but it is possible to do it also by SQL command (e.g. ALTER TABLE ...).

UPDATE: Another candidate for extension is attribute name in table django_content_type, and maybe other attributes in other tables, when you'll get that error message, just play with it and you will find out what to extend.

1 komentár:

Archív blogu