Ticket #24611: update_uuid_fk.patch

File update_uuid_fk.patch, 2.7 KB (added by Jay Wineinger, 4 years ago)
  • django/db/models/sql/compiler.py

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    index 7ea9ef9..59220db 100644
    a b class SQLUpdateCompiler(SQLCompiler): 
    10121012                    raise FieldError("Aggregate functions are not allowed in this query")
    10131013            elif hasattr(val, 'prepare_database_save'):
    10141014                if field.remote_field:
    1015                     val = val.prepare_database_save(field)
     1015                    val = field.get_db_prep_save(val.prepare_database_save(field),
     1016                                                 connection=self.connection)
    10161017                else:
    10171018                    raise TypeError("Database is trying to update a relational field "
    10181019                                    "of type %s with a value of type %s. Make sure "
  • tests/update/models.py

    diff --git a/tests/update/models.py b/tests/update/models.py
    index e0b391d..08706ef 100644
    a b  
    22Tests for the update() queryset method that allows in-place, multi-object
    33updates.
    44"""
     5import uuid
    56
    67from django.db import models
    78from django.utils import six
    class Foo(models.Model): 
    5051
    5152class Bar(models.Model):
    5253    foo = models.ForeignKey(Foo, to_field='target')
     54
     55
     56class UUIDPK(models.Model):
     57    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
     58
     59
     60class UUIDRelation(models.Model):
     61    relation = models.ForeignKey(UUIDPK)
  • tests/update/tests.py

    diff --git a/tests/update/tests.py b/tests/update/tests.py
    index 1ed316c..a0b5934 100644
    a b from __future__ import unicode_literals 
    22
    33from django.test import TestCase
    44
    5 from .models import A, B, D, Bar, DataPoint, Foo, RelatedPoint
     5from .models import A, B, D, Bar, DataPoint, Foo, RelatedPoint, UUIDPK, UUIDRelation
    66
    77
    88class SimpleTest(TestCase):
    class AdvancedTests(TestCase): 
    138138        self.assertEqual(bar_qs[0].foo_id, a_foo.target)
    139139        bar_qs.update(foo=b_foo)
    140140        self.assertEqual(bar_qs[0].foo_id, b_foo.target)
     141
     142
     143class UUIDRelationTests(TestCase):
     144
     145    def setUp(self):
     146        self.u1 = UUIDPK.objects.create()
     147        u2 = UUIDPK.objects.create()
     148        UUIDRelation.objects.create(relation=u2)
     149
     150    def test_update_uuid_fk_with_model_instance(self):
     151        """
     152        Update of a FK to a model with UUID pk using a model instance
     153        """
     154        UUIDRelation.objects.update(relation=self.u1)
     155
     156    def test_update_uuid_fk_with_uuid_instance(self):
     157        """
     158        Update of a FK to a model with UUID pk using a UUID instance
     159        """
     160        UUIDRelation.objects.update(relation=self.u1.pk)
Back to Top