UUIDs and data truncation warnings

183 views
Skip to first unread message

Paul Walsh

unread,
Feb 19, 2012, 11:37:56 PM2/19/12
to pywe...@googlegroups.com
I have added uuids to most models in an app I am working on.

I have done two implementations, one using https://github.com/dcramer/django-uuidfield and another using a simple function to create a uuid like so:

def make_uuid():

    return str(uuid.uuid4())


and invoking it in models.py as the default on a character field, to make a uuid for each instance.


In both cases, I get warning when saving new instances that the data has been truncated. Even though it seems I can still use the truncated key, this warning makes, for example, my init fixtures fail as soon as they insert a uuid.


I found that, after creating my tables with the ORM, if I then go and manually adjust the character limit on a uuid field (say, from 36 to 40), then I don't get warnings, but I haven't tested this too far. However, I did test setting the character limit at 40 in advance, on a 36 character uuid field, and then building the tables, but this still ends up in data truncation warnings.


I am using MySQL. Any suggestions to implement uuid in a safe way, or what might be going wrong?



Pratik Mandrekar

unread,
Jun 18, 2012, 9:37:06 AM6/18/12
to pywe...@googlegroups.com
Hi Paul,

I faced a similar issue and then realized what was wrong. The 36 to 40 happens because of the '-' in the uuids if you directly try to force them to unicode or string. A better way to do this is by using uuid.UUID4().hex or any other method or uuid object with the .hex on it which exactly gives 32 characters.

So this,

def make_uuid():

    return str(uuid.uuid4())


should be:


def make_uuid():

    return uuid.uuid4().hex



Pratik Mandrekar
Reply all
Reply to author
Forward
0 new messages