Random search in Django but ignored in Mysql

I have a field in a Django model to store unique (hash values). It turns out that the database (MySQL / inno) does not perform case-sensitive searches of this type (VARCHAR), even if I explicitly tell Django that it does case-sensitive searches Document.objects.get(hash__exact="abcd123"). So "abcd123" and "ABcd123" are back, which I don’t want.

class document(models.Model):
   filename    = models.CharField(max_length=120)
   hash        = models.CharField(max_length=33 )

I can change the "hash field" to BinaryField, so in the database it becomes LONGBLOB, and it does a case-sensitive search (and it works). However, this does not seem to me very effective. Is there a better way (in Django) to do this, for example, add "utf8 COLLATE"? or what would be the right field in this situation? (yes, I know that I could use PostgreSQL instead.)

+4
source share
3 answers

As @ dan-klasson mentioned, standard non-binary string comparisons are not case sensitive by default; pay attention at _cithe end latin1_swedish_ci, it means case-sensitive. You can, as Dan said, create a database using sorting and case sensitive characters.

You may also be interested to know that you can always create a separate table or even set only one column to use a different mapping (for the same result). And you can also change the creation of messages for sorting, for example, for a table:

ALTER TABLE documents__document CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

, / /, Django . , , - , :

Document.objects.raw("SELECT * FROM documents__document LIKE '%s' COLLATE latin1_bin", ['abcd123'])
+2

MySQL - latin1_swedish_ci, . , . :

CREATE DATABASE database_name CHARACTER SET utf8;
+3

. __exact, :

Document.objects.get(hash__exact="abcd123")

, , __iexact.

0

All Articles