Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - limit key size on unique_together columns

Using MySQL, I am trying to have a table with a composite key of multiple fields.

The issue is that some of the fields are large (255 - 1024 length), if I try to run the migration, I will get:

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

Instead of increasing the DB's key length (or changing some other DB / table settings), I found out that I can just limit the value of the field used as a key in the migration file so that it remains within the max key length, like this:

ALTER TABLE <table> ADD UNIQUE KEY `<table>_composite_key` (`col1`, `col2`(75), `col3`, `col4`, `col5`(150));

However, this is an issue if I were to change the db engine, as that syntax might not be compatible with other.

So I'm wondering is there a way to impose limit for each field in unique_together?

Thanks in advance!

like image 861
swlim Avatar asked Dec 31 '18 14:12

swlim


1 Answers

Now Django do not support constraints, but in near future will do it. Now you can add TODO for updating it when Django 2.2 will be released. Now there are not many API for constraints.

See: https://docs.djangoproject.com/en/2.2/ref/models/constraints/

It is not answer for your question but it can help you for figuring out with this.

like image 161
Andrey Berenda Avatar answered Oct 14 '22 17:10

Andrey Berenda