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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With