#26139: UUIDField value must be declared "binary" to avoid MySQL warning
-------------------------------------+-------------------------------------
     Reporter:  Guilhem Bichot       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.9
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  MySQL                |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Chris Foresman):

 Replying to [comment:3 Tim Graham]:
 > Yes, Claude is correct. Django's `UUIDField` uses `char(32)` -- the
 `binary(16)` representation is a third-party implementation. The warnings
 I'm seeing for Django's test suite are for `BinaryField` (created #26140).

 I've been researching this; is there a reason we _don't_ use `BINARY(16)`
 for MySQL? It should be massively [http://mysqlserverteam.com/storing-
 uuid-values-in-mysql-tables/ more efficient] in storage and index space. I
 think if I subclass the existing `UUIDField`, this should be pretty close
 to working (I think I need to override `__init__` to set the `max_length`
 to 16:

 {{{
 class MySQLUUIDField(UUIDField):

     def get_internal_type(self):
         return "BinaryField"

     def get_db_prep_value(self, value, connection, prepared=False):
         if value is None:
             return None
         if not isinstance(value, uuid.UUID):
             value = self.to_python(value)

         if connection.features.has_native_uuid_field:
             return value
         return value.bytes

     def to_python(self, value):
         if value is not None and not isinstance(value, uuid.UUID):
             try:
                 return uuid.UUID(bytes=value)
             except (AttributeError, ValueError):
                 raise exceptions.ValidationError(
                     self.error_messages['invalid'],
                     code='invalid',
                     params={'value': value},
                 )
         return value
 }}}

 I'm taking some liberties and not accounting for every edge case, but I
 think that's enough to work with the existing MySQL backend as written,
 though ideally I'd like the backend to presume `BINARY(16)` and the field
 definition could act accordingly. Looking at Oracle and sqlite, those DBs
 use a `BLOB` type for binary data, so I'm not sure if there is any benefit
 or not using that type for a UUID.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/26139#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.b5fb564303a6d33a3688daae85c95015%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to