dcramer/django-uuidfield — pull request #27 by MikeAmy (2014-02-14 07:00:36+00:00)
Title: Remove hyphens from UUIDs passed into queries

When performing queries, it is easy to pass in a UUID with hyphens, as this is the common format of UUIDs.

Consider this example:

class ModelUsingUUID(models.Model):
uuid = UUIDField()

def sync_remotely_created_instance(uuid_value, **other_data):
if ModelUsingUUID.objects.filter(uuid=uuid_value).count() == 0:
ModelUsingUUID.objects.create(uuid=uuid_value, **other_data)
else:
# update the existing instance
...

If it happens that uuid_value is sent from the remote system with hyphens, depending on the database, the lookup can fail, as the database may or may not understand that the hyphens should be ignored. Subsequently, the creation of the new instance may fail, as the uniqueness constraint will be violated.

It could be argued that it is the responsibility of the developer writing the query to ensure that the hyphens are removed, however, seeing as there is in fact no ambiguity with or without the hyphens (hyphens have no meaning by themselves in a UUID), it should always be safe to ignore them, and as bugs similar to the one above may pass silently (e.g. if creation of the instance were done at a different time to the existence check), it is probably safer to simply remove the dashes to avoid these kinds of silent bugs altogether.