django-celery icon indicating copy to clipboard operation
django-celery copied to clipboard

Mysql error 1071 : Specified key was too long; max key length is 767 bytes

Open pjambet opened this issue 13 years ago • 18 comments

I'm using mysql with encoding set as utf8mb4.

Because of that, an index on a column larger than 191 characters will trigger a mysql error 1071 when migrating djcelery

django.db.utils.DatabaseError: (1071, 'Specified key was too long; max key length is 767 bytes')

I've tried tons of different stuff in order to make that work, but the only working solution I came up with was to fork the project, and reduce all columns that caused the error to 191 chars.

Any help here would be extremely appreciated !

pjambet avatar Jul 16 '13 22:07 pjambet

Ouch, I'm not quite sure how this can be easily fixed. Maybe there is some setting in Django to set a max length for the field? You could monkey patch CharField directly I guess.

ask avatar Sep 03 '13 12:09 ask

The only alternative I can think of that django-celery could do to fix this would be to have an environment variable to set a limit, something like CELERY_DB_CHARMAX. But that would be a pretty awkward solution and I really feel that this is something that belongs in Django.

ask avatar Sep 03 '13 12:09 ask

The field(s) in question are already a bit arbitrary in length. There shouldn't be an issue with truncating existing users' data as this solution requires a modification to the migration itself. Also, I noticed what I believe to be a small typo with @pjambet's fix: https://github.com/ericbuehl/django-celery/commit/1a946a78af95b8c70ab7323d547115b7944017f6

Is this acceptable to be pulled upstream?

ericbuehl avatar Oct 23 '13 23:10 ericbuehl

And another field: https://github.com/ericbuehl/django-celery/commit/092700c2dba81bc1b8f53d554a6f3339e2db171a

ericbuehl avatar Oct 24 '13 01:10 ericbuehl

I know +1 isn't going to solve much, but I've just run into this while deploying a django-based click-click thingie (uhm, software, app and program are too mainstream now I guess).

All the uniquely constrained text/varchar fields are affected due to InnoDB's limit for on-page column size. (Anything larger than the 767 bytes goes into a new page/block, which is a no-no for indexes, hence the restriction.)

A potential workaround is to recommend using the DYNAMIC (or COMPRESSED) row format and the innodb_large_prefix option (which pushes this limit to 3072 bytes). However, if people tinker this much with their RDBMS installation they should also mind that lowering the page size lowers this extended limit too proportionally.

PAStheLoD avatar Oct 24 '14 08:10 PAStheLoD

This is still an issue. pjambet has fixed a few lengths in his fork, but not 100%, so I had to fork from ericbuehl's fork of that.

As I understand it, django-celery is still useful despite what the README says. From http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html:

"If you want to store task results in the Django database then you still need to install the django-celery library for that (alternatively you can use the [unstable] SQLAlchemy result backend)."

We generally ignore results, but want to capture certain task results the simplest way possible.

trevoriancox avatar Dec 15 '15 15:12 trevoriancox

I’ve published a variant of the current release with appropriate patches; it’s available at roi-com-au/[email protected].

If anyone wants to use this, the requirements.txt line would be

git+git://github.com/roi-com-au/[email protected]#egg=django-celery==3.1.17-mysql-utf8mb4

chris-morgan avatar Feb 22 '16 02:02 chris-morgan

How can I fixed it? The only way to fix this issue is modify models.py and 0001_initial.py by myself?

mingforpc avatar Apr 05 '16 13:04 mingforpc

@JianMingZhuo I gave a patched version of 3.1.17, with the instructions in my comment above. Add such a line to your requirements.txt and you’re set.

chris-morgan avatar Apr 05 '16 22:04 chris-morgan

@chris-morgan Thanks

mingforpc avatar Apr 06 '16 01:04 mingforpc

I would request to send a PR

auvipy avatar Dec 14 '16 10:12 auvipy

@auvipy Wait, you’re closing the issue when it’s not fixed? That’s not how issues are supposed to work.

chris-morgan avatar Dec 14 '16 14:12 chris-morgan

@chris-morgan I requested you to send a PR as you got a patched version

auvipy avatar Dec 14 '16 16:12 auvipy

Note that the patch there is not suitable for application; I rewrote history there rather than creating a new migration. I’m not sure what the desirable behaviour would be in case of overflow when shortening 255 characters to 191. And cutting it down for everyone just because of MySQL’s incompetence in matters of Unicode seems rather sad, if the chance of desiring a value of over 191 characters is non-zero. I’ve got no idea whether that is the case or not. Also I’m not working on that project any more and may well never need to use django-celery with MySQL again.

chris-morgan avatar Dec 15 '16 07:12 chris-morgan

For avoiding this error in MariaDB just specify utf8_general_ci at creation time:

create database database_name default CHARACTER set utf8 default COLLATE utf8_general_ci;

If you can't create a new database, just convert it. It will avoid this error.

datacorder avatar Jun 19 '17 18:06 datacorder

still having the issue...

ochanje210 avatar Jan 12 '18 02:01 ochanje210

Upgrade mysql to 5.7 and try migrate again.

wget https://dev.mysql.com/get/mysql-apt-config_0.8.1-1_all.deb  
sudo dpkg -i mysql-apt-config_0.8.1-1_all.deb  
sudo apt-get update
sudo apt-get install mysql-server

elfgzp avatar Jul 30 '18 05:07 elfgzp

Closing this issue since it's now been a few years and I haven't worked with django-celery in a while

pjambet avatar Jun 14 '21 14:06 pjambet