Using unique=True on MySQL fails due to missing "key length"
flyte opened this issue · comments
Ellis Percival commented
When creating (in a migration) an encrypted field that has unique=True
, the MySQL backend gives the following error:
MySQLdb._exceptions.OperationalError: (1170, "BLOB/TEXT column 'email_new' used in key specification without a key length")
This SO post explains the reason behind the error: https://stackoverflow.com/a/1827099/1791183
1, Is this something that django-cryptography can fix?
2. Will unique=True
even work on an encrypted field?
3. Is there a workaround?
Paul Stiverson commented
To question 2 I say no; it might work on some databases, but generally no.
To question3, yes. The workaround is a little ugly, but it works. Decrypt each row and compare it to new candidate row and see if there is a collision:
for row in EncryptedModel.objects.all():
if row.unique_attribute == new_unique_attribute:
raise Exception("bruh... are you fr advocating for a full record search?? yes, but only because there is no alternative")