Closed nijel closed 1 month ago
Possible solutions I can see:
Manually create the index for MySQL with length specified.
Needs to be implemented in the migration code, but should be doable.
Use CharField
instead and store hexdigest()
instead of digest()
there.
A bit slower, but works with any database.
Use Index(SHA256("credential_id"))
and then get using database instead of calculating SHA256 in Python.
Something like .annotate(credential_id_sha256=SHA256(F("credential_id")).get(credential_id_sha256=SHA256(credential_id))
can be used inside get_by_credential_id
.
The index would be skipped by Django on old MySQL and MariaDB, but will perform good elsewhere and use standard Django syntax. Moreover, this removes the need to calculate something manually in save()
.
I can prepare a pull request, but I'd like to hear opinion on the preferred approach first.
@nijel thanks for listing those suggestion so clearly, I happened to have some time available today so I tried out the third solution as it was most appealing to me because - as you mentioned - it avoids calculating the hash in the save()
method.
I ended up discovering that cryptographic functions like SHA256
are not always available. On Postgres for example, it requires the pgcrypto
extension to be enabled which I consider too much of a hassle to warrant going with this solution.
Ah, I use just MD5 so far and never hit that it would be unavailable, I thought it's the case for all of them.
Migrating on MySQL fails with: