1

What do I keep getting this error on my django app? I am using mysql on webfaction which is standard and I have set this to UTF8 - I really cant put my head around this

"Failed to install index for feeds.Genre model: Specified key was too long; max key length is 767 bytes"

this is my model

from django.db import models
import uuid
import os


class Messages(models.Model):
    name = models.CharField(max_length=255)
    message = models.CharField(max_length=255)
    show = models.DateTimeField(auto_now_add=True)
    hide = models.DateTimeField()
    def __unicode__(self):
        return self.name 





class Genre(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=300)
    meta = models.TextField(max_length=300)
    description = models.TextField(max_length=300)
    created = models.DateTimeField(auto_now_add=True)
    listing = models.BooleanField(default=True)
    def __unicode__(self):
        return self.name


class Category(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=300)
    meta = models.TextField(max_length=300)
    description = models.TextField(max_length=300)
    created = models.DateTimeField(auto_now_add=True)
    listing = models.BooleanField(default=True)
    def __unicode__(self):
        return self.name



class Images(models.Model):
    name = models.CharField(max_length=50)
    def get_file_path(instance, filename):
        ext = filename.split('.')[-1]
        filename = "%s.%s" % (uuid.uuid4(), ext)
        return os.path.join( 'images' , filename)
    image = models.ImageField(upload_to = get_file_path, null=True, blank=True)
    def __unicode__(self):
        return self.name  




class Gallery(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=300)
    meta = models.TextField(max_length=300)
    description = models.TextField(max_length=300)
    images = models.ManyToManyField(Images)
    def __unicode__(self):
        return self.name    


class Article(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=300)
    category = models.ManyToManyField(Category)
    link = models.URLField(max_length=255)
    meta = models.URLField(max_length=255)
    description = models.TextField(max_length=300)
    content = tinymce_models.HTMLField()
    source_name = models.CharField(max_length=50)
    source_link=models.URLField()
    created = models.DateTimeField(auto_now_add=True)
    listing = models.BooleanField(default=True)
    def get_file_path(instance, filename):
        ext = filename.split('.')[-1]
        filename = "%s.%s" % (uuid.uuid4(), ext)
        return os.path.join( 'images' , filename)
    image = models.ImageField(upload_to = get_file_path, null=True, blank=True)
    def __unicode__(self):
        return self.name
user3482036
  • 109
  • 11

1 Answers1

1

I believe that this is an issue with the length of VARCHAR fields in MySQL.

You are using a SlugField, in the background this is also using VARCHAR the same way that CharField is. The same length limits apply for both fields, so setting max_length=300 in your slug fields is a problem (300 * 3 bytes is 900 bytes, which is over the max of 767, as the error suggests. If you're wondering where 767 comes from, 255 chars * 3 bytes per char is 765 bytes + 2 bytes prefix).

Reduce the max_length attribute of your SlugField fields to 255 and you're good to go.

Geekfish
  • 2,173
  • 23
  • 28
  • 1
    Isn't 255 chars times 3 bytes/char = 765 bytes? – uranusjr May 12 '14 at 16:06
  • 1
    Ha, you're absolutely right, I don't know how I did this :P -edit: Actually I did some reading on mysql varchar types and "... VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data." so 765 + 2 bytes as prefix. http://dev.mysql.com/doc/refman/5.0/en/char.html – Geekfish May 12 '14 at 16:57