#1170 – Blob/Text Column ‘Variable’ Used In Key Specification Without A Key Length In Django
If you having 1170 – Blob/Text Column ‘Variable’ Used In Key Specification error and you have search for solution you didn’t see, then welcome at the end of this article you will be able to solve your problem.
How to solve #1170 – BLOB/TEXT column ‘variable’ used in key specification without a key length in django
The error message you have encountered (#1170) usually comes up in MySQL when you’re trying to create an index on a column of data type BLOB or TEXT without specifying a key length.
Or some times you might been on sqLite then you might want to migrate to MySql then the error will pop up if you have set a unique constraint without specifying a key length.
Say for example you have added unique key in slug then you must specify a key length in the title, also you must also specify a key length in the slug it self .
lets say you have a model then with title and slug then you want a unique constrant in such a way that all your slug would be unique then to solve this
from django.db import models
from django.utils.text import slugify
class Blog(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True, blank=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
Since you have specify a key length in the title the error will disappear
To resolve this issue, you need to ensure that you provide a key length when creating an index on columns of these data types.
You don’t need to enter into your db to resolve this issue, just make sure that you specify a key length in some of the fields you have like title, Slug, category etc
Here’s an example of how you can modify your index creation statement