diff --git a/AUTHORS b/AUTHORS index c1e00923f6..79f5ae1b3b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -314,6 +314,7 @@ answer newbie questions, and generally made Django that much better: Jason Yan Javier Mansilla Jay Parlar + Jay Wineinger J. Clifford Dyer jcrasta@gmail.com jdetaeye diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 7ea9ef9067..65c01fb642 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -1012,7 +1012,10 @@ class SQLUpdateCompiler(SQLCompiler): raise FieldError("Aggregate functions are not allowed in this query") elif hasattr(val, 'prepare_database_save'): if field.remote_field: - val = val.prepare_database_save(field) + val = field.get_db_prep_save( + val.prepare_database_save(field), + connection=self.connection, + ) else: raise TypeError("Database is trying to update a relational field " "of type %s with a value of type %s. Make sure " diff --git a/docs/releases/1.8.1.txt b/docs/releases/1.8.1.txt index d942d32842..988a532a89 100644 --- a/docs/releases/1.8.1.txt +++ b/docs/releases/1.8.1.txt @@ -38,3 +38,6 @@ Bugfixes * Prevented arbitrary file inclusions in :mod:`~django.contrib.admindocs` (:ticket:`24625`). + +* Fixed a crash with ``QuerySet.update()`` on foreign keys to instances with + ``uuid`` primary keys (:ticket:`24611`). diff --git a/tests/model_fields/test_uuid.py b/tests/model_fields/test_uuid.py index 28e11914a1..7ce98a0852 100644 --- a/tests/model_fields/test_uuid.py +++ b/tests/model_fields/test_uuid.py @@ -129,3 +129,20 @@ class TestAsPrimaryKey(TestCase): RelatedToUUIDModel.objects.create(uuid_fk=pk_model) related = RelatedToUUIDModel.objects.get() self.assertEqual(related.uuid_fk.pk, related.uuid_fk_id) + + def test_update_with_related_model_instance(self): + # regression for #24611 + u1 = PrimaryKeyUUIDModel.objects.create() + u2 = PrimaryKeyUUIDModel.objects.create() + r = RelatedToUUIDModel.objects.create(uuid_fk=u1) + RelatedToUUIDModel.objects.update(uuid_fk=u2) + r.refresh_from_db() + self.assertEqual(r.uuid_fk, u2) + + def test_update_with_related_model_id(self): + u1 = PrimaryKeyUUIDModel.objects.create() + u2 = PrimaryKeyUUIDModel.objects.create() + r = RelatedToUUIDModel.objects.create(uuid_fk=u1) + RelatedToUUIDModel.objects.update(uuid_fk=u2.pk) + r.refresh_from_db() + self.assertEqual(r.uuid_fk, u2)