Seb
Seb

Reputation: 61

Django UUIDField Truncates from varchar(36) to char(32)

After upgrading to Django 1.8.3, I needed to change from using django_extensions.db.fields.UUIDField to the new django.db.models.UUIDField.

The issue is that the old one was defined as varchar(36) with hyphens and the new one is char(32) without hyphens. I definitely see the size benefits of not having these hyphens in there.

I gave it a go and, as I feared, the data in the column gets truncated.

When I check the definition of the field, it is initialized with:

def __init__(self, verbose_name=None, **kwargs):
    kwargs['max_length'] = 32
    super(UUIDField, self).__init__(verbose_name, **kwargs)

This definition blocks any override of the max_length.

Of course I can update every single UUIDFields I have to remove the hyphens but this is quite a long process because my tables are quite big. For information, this is the kind of query I would have to run:

UPDATE myTable
SET uuid_field = REPLACE(uuid_field, '-', '');

So my question is, do you have any better idea than running a huge update for every rows of every tables with UUIDFields?

Upvotes: 2

Views: 1810

Answers (2)

Dougyfresh
Dougyfresh

Reputation: 616

I faced this same issue. Ended up just taking the easy route and defining my own model field.

from django.db import models


class UUIDField(models.UUIDField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 36
        super(models.UUIDField, self).__init__(*args, **kwargs)

Upvotes: 0

Seb
Seb

Reputation: 61

Oh well, it's way too painful to update every UUID fields in the database, I'm reimplementing something similar to the old django_extensions.db.fields.UUIDField.

Django's first aim is Postgres which has its own UUID field type (without hyphens) but MySQL doesn't.

Django's new UUIDField works well if you are creating a new field but it's a pain if you already have data.

Upvotes: 2

Related Questions