1

My model:

from django.db import models
from django.contrib.postgres.fields import JSONField

class MyModel(models.Model):
    data = JSONField(blank=True, null=True)

I have created some objects and filled "data" field with some JSONs

Then I tried to create index for "data" field

class MyModel(models.Model):
    data = JSONField(blank=True, null=True, db_index=True)

Make migrations

python manage.py makemigrations

Migrate

python manage.py migrate

Long traceback, and last line is:

django.db.utils.OperationalError: index row requires 8336 bytes, maximum size is 8191

How can I fix it?

1 Answer 1

3

As you can see from this answer, this is a limitation of the b-tree index. There is no simple solution to this. Indexing an entire JSONField also doesn't really make any sense. For what reason are you attempting to index the JSONField in the first place?

My recommendation would be to remove this index, as I doubt it is required. You may wish to try a more complex index, but Django's built in db_index is not capable of this. You'd have to create a more custom approach based on your situation.

Sign up to request clarification or add additional context in comments.

1 Comment

Big thx for information. I just wanted to make performance test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.