Skip to content

PostgreSQL throws DataError when trying to retrieve objects with invalid UUID #46

Closed
@lingxiaoyang

Description

@lingxiaoyang

The following code will trigger the problem with PostgreSQL backend:

SomeModel.objects.get(uuid="invalid_uuid")

An example traceback:

/.../lib/python2.7/site-packages/django/db/backends/utils.pyc in execute(self, sql, params)
     63                 return self.cursor.execute(sql)
     64             else:
---> 65                 return self.cursor.execute(sql, params)
     66 
     67     def executemany(self, sql, param_list):

DataError: invalid input syntax for uuid: "invalid_uuid"
LINE 1: ... "some_model" WHERE "some_model"."uuid" = 'invalid_u...
                                                     ^

PostgreSQL has a UUID type and throws an error with badly formatted UUID. However, it is not the expected behavior of an API. When user provides an invalid UUID, the API in most cases is expected to return 404, instead of 500 caused by this uncaught exception.

The problem lies in method get_db_prep_value. If the provided UUID is not valid, the method should return an arbitrary valid UUID that matches nothing in the database.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions