Hey there!
I have a model that contains a JSONField which I’m trying to add to a UniqueTogether constraint like this:
class AppCommand(models.Model):
guild_id = models.BigIntegerField(null=True)
data = models.JSONField()
class Meta:
constraints = (
models.UniqueConstraint(fields=("guild_id", "data"), name="guild_data_unique_together"),
)
However, the resulting index from Postgres is "guild_data_unique_together" UNIQUE CONSTRAINT, btree (guild_id, data), B-tree isn’t an appropriate index type for jsonb, and I’m of course hit with the following error when trying to do something:
django.db.utils.OperationalError: index row requires 8280 bytes, maximum size is 8191
Is there a way to specify to Django which index type to use for its constraint?
I’ve also read online[1] that you can use a hashing function instead, but I’m worried about the performance cost of doing this on large JSON objects? Postgres seems to already have a very good equality operator on jsonb objects.
Of course if there are better ways to do this, feel free to suggest alternatives!