Hi,

My use case is exactly similar to this with one additional condition that I 
want this column to serve as Primary Key of my table. Now when I use the 
following code:

class BinaryTable(models.Model):
    binary_hash = models.BinaryField(primary_key=True)
    comments = models.CharField(max_length=70)

    class Meta:
        db_table = 'binary_table'
       
I get the error code (1170, "BLOB/TEXT column 'binary_hash' used in key 
specification without a key length")
The behaviour is same if I specify unique=True instead of primary_key=True.

Now, one workaround is that from SHA256Field we return "BINARY(32) PRIMARY 
KEY"
The problem with this is that django sees no primary_key=True column while 
parsing the model declaration and assigns an automatic primary key and then 
raises the error that there are 2 primary keys. Any ideas how I can achieve 
primary key constraint on this custom field?

On Thursday, 13 July 2017 22:04:49 UTC+5:30, winke...@hornetsecurity.com 
wrote:
>
> On Thursday, July 13, 2017 at 4:06:38 PM UTC+2, Tom Evans wrote:
>  [...]
>
>> Guido: 
>>
>> You can make your own custom database types quite simply (completely 
>> untested): 
>>
>> class FixedSizedBinaryField(models.BinaryField): 
>>   def __init__(self, num_bytes=None, *args, **kwargs): 
>>     self.nbytes = num_bytes 
>>     super(FixedSizedBinaryField, self).__init__(*args, **kwargs) 
>>
>>   def deconstruct(self): 
>>     name, path, args, kwargs = super(FixedSizedBinaryField, 
>> self).deconstruct() 
>>     kwargs[''num_bytes'] = self.nbytes 
>>     return name, path, args, kwargs 
>>
>>   def db_type(self, connection): 
>>     return "binary(%d)" % self.nbytes 
>>
>> For more details, check out the docs on custom fields: 
>>
>> https://docs.djangoproject.com/en/1.11/howto/custom-model-fields/ 
>>
>
> Thanks for the suggestion. I am now working with this custom field class 
> (I actually found the relevant documentation and the solution myself in the 
> meantime. It's almost identical to yours, except without a configurable 
> length):
>
> class SHA256Field(models.BinaryField):
>     def __init__(self, *args, **kwargs):
>         kwargs['max_length'] = 32
>         super(SHA256Field, self).__init__(*args, **kwargs)
>
>     def deconstruct(self):
>         name, path, args, kwargs = super(SHA256Field, self).deconstruct()
>         del kwargs["max_length"]
>         return name, path, args, kwargs
>
>     def db_type(self, connection):
>         return "BINARY(32)"
>
> This works very well for me - in MySQL/MariaDB. I haven't tested it with 
> any other database systems.
>
> Regards,
>
> Guido
>  
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c817b490-8795-47c1-9146-f952b853f57c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to