#36073: Custom field without get_db_prep_value() errors on updates if part of
composite primary key
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  (none)
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:  invalid
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

 * resolution:   => invalid
 * status:  new => closed

Comment:

 Yes, it's the same for regular primary keys which makes me think whether
 this is not valid
 It's possible that the test code from serializers that inspired this is
 not fully defined.
 Jacob if you can add more information we can look at this again

 (for reference, same thing for regular primary keys)
 {{{#!diff
 diff --git a/tests/custom_pk/fields.py b/tests/custom_pk/fields.py
 index 2d70c6b6dc..3908b426c9 100644
 --- a/tests/custom_pk/fields.py
 +++ b/tests/custom_pk/fields.py
 @@ -20,7 +20,7 @@ class MyWrapper:
          return self.value == other


 -class MyWrapperField(models.CharField):
 +class MyWrapperFieldWithoutGetDbPrepValue(models.CharField):
      def __init__(self, *args, **kwargs):
          kwargs["max_length"] = 10
          super().__init__(*args, **kwargs)
 @@ -51,6 +51,7 @@ class MyWrapperField(models.CharField):
              return str(value)
          return value

 +class MyWrapperField(models.CharField):
      def get_db_prep_value(self, value, connection, prepared=False):
          if not value:
              return
 diff --git a/tests/custom_pk/models.py b/tests/custom_pk/models.py
 index 000ea7a29d..75b1031e10 100644
 --- a/tests/custom_pk/models.py
 +++ b/tests/custom_pk/models.py
 @@ -7,7 +7,7 @@ this behavior by explicitly adding ``primary_key=True`` to
 a field.

  from django.db import models

 -from .fields import MyAutoField, MyWrapperField
 +from .fields import MyAutoField, MyWrapperField,
 MyWrapperFieldWithoutGetDbPrepValue


  class Employee(models.Model):
 @@ -40,3 +40,7 @@ class Foo(models.Model):

  class CustomAutoFieldModel(models.Model):
      id = MyAutoField(primary_key=True)
 +
 +
 +class PKWithoutGetDbPrepValue(models.Model):
 +    id = MyWrapperFieldWithoutGetDbPrepValue(primary_key=True)
 diff --git a/tests/custom_pk/tests.py b/tests/custom_pk/tests.py
 index 5f865b680a..3bfd32c29c 100644
 --- a/tests/custom_pk/tests.py
 +++ b/tests/custom_pk/tests.py
 @@ -2,7 +2,7 @@ from django.db import IntegrityError, transaction
  from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature

  from .fields import MyWrapper
 -from .models import Bar, Business, CustomAutoFieldModel, Employee, Foo
 +from .models import Bar, Business, CustomAutoFieldModel, Employee, Foo,
 PKWithoutGetDbPrepValue


  class BasicCustomPKTests(TestCase):
 @@ -164,6 +164,11 @@ class CustomPKTests(TestCase):
          Business.objects.create(name="Bears")
          Business.objects.create(pk="Tears")

 +    def test_custom_composite_pk_save_force_update(self):
 +        obj = PKWithoutGetDbPrepValue()
 +        obj.id = MyWrapper("1")
 +        obj.save()  # passes with force_insert=True
 +
      def test_unicode_pk(self):
          # Primary key may be Unicode string.
          Business.objects.create(name="jaźń")
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36073#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194501335c7-829cb2b2-2f0e-4b70-aff5-f7bf05be4ae7-000000%40eu-central-1.amazonses.com.

Reply via email to