Opened 9 years ago

Closed 9 years ago

#24859 closed Cleanup/optimization (fixed)

UUIDField does not validate query value

Reported by: Alex Rothberg Owned by: Tim Graham <timograham@…>
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: hi@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

I have two models one with an int pk and one with a UUIDField pk:

class Bar(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid4)

class Bar2(models.Model):
    pass

If I try to get these using a value of {} they fail differently. The model with the int pk:

>>> Bar2.objects.all().get(pk={})
TypeError: int() argument must be a string or a number, not 'dict'

whereas:

>>> Bar.objects.all().get(pk={})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/models/query.py", line 328, in get
    num = len(clone)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/models/query.py", line 144, in __len__
    self._fetch_all()
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/models/query.py", line 238, in iterator
    results = compiler.execute_sql()
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/alex/.virtualenvs/TestPG/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
    return Database.Cursor.execute(self, query, params)
InterfaceError: Error binding parameter 0 - probably unsupported type.

This inconsistency leads to issues like: https://github.com/tomchristie/django-rest-framework/issues/2970

Change History (10)

comment:1 by Tim Graham, 9 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Accepting in a similar vein to #24319. Here's a test that can be used to reproduce the issue:

  • tests/model_fields/test_uuid.py

    diff --git a/tests/model_fields/test_uuid.py b/tests/model_fields/test_uuid.py
    index 21c2869..95d47b5 100644
    a b class TestSaveLoad(TestCase):  
    3737        loaded = NullableUUIDModel.objects.get()
    3838        self.assertEqual(loaded.field, None)
    3939
     40    def test_pk_validated(self):
     41        PrimaryKeyUUIDModel.objects.get(pk={})
     42
    4043    def test_wrong_value(self):
    4144        self.assertRaisesMessage(
    4245            ValueError, 'badly formed hexadecimal UUID string',

comment:2 by Tim Graham, 9 years ago

Summary: UUIDField does not Properly Raise TyperErrorUUIDField does not validate query value

comment:3 by Cole Maclean, 9 years ago

Has patch: set

comment:4 by Cole Maclean, 9 years ago

Cc: hi@… added

comment:5 by Marc Tamlyn, 9 years ago

Cc: hi@… removed
Easy pickings: set
Patch needs improvement: set

comment:6 by Marc Tamlyn, 9 years ago

Cc: hi@… added

comment:7 by Cole Maclean, 9 years ago

OK, patch revised.

comment:8 by Steadman, 9 years ago

Owner: changed from nobody to Steadman
Status: newassigned

comment:9 by Steadman, 9 years ago

Owner: Steadman removed
Status: assignednew

comment:10 by Tim Graham <timograham@…>, 9 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 20ff296:

Fixed #24859 -- Made QuerySet.get() with UUIDField raise TypeError on bad value.

For consistency with AutoField.

Note: See TracTickets for help on using tickets.
Back to Top