#36431: values() query on ForeignObject discards additional columns
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  4.2                  |                 Severity:  Normal
     Keywords:  composite primary    |             Triage Stage:
  key                                |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 For a multi-column field using `ForeignObject` as modeled
 [https://docs.djangoproject.com/en/5.2/topics/composite-primary-key
 /#composite-primary-keys-and-relations here], `.values("user")` will
 discard columns after the first, and `values("user", "integer")` will
 select the second column of `user` into the `integer` namespace.

 Here's a failing test for tests/composite_pk/test_values.py:
 {{{#!py
     def test_foreign_object_values(self):
         Comment.objects.create(id=1, user=self.user_1, integer=42)
         values = list(Comment.objects.values("user", "integer"))
         self.assertEqual(values[0]["integer"], 42)
 }}}
 {{{
 ======================================================================
 FAIL: test_foreign_object_values
 (composite_pk.test_values.CompositePKValuesTests.test_foreign_object_values)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/.../django/tests/composite_pk/test_values.py", line 217, in
 test_foreign_object_values
     self.assertEqual(values[0]["integer"], 42)
     ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
 AssertionError: 1 != 42
 }}}

 And a similar rough test for foreign_objects/tests.py that fails on Django
 4.2, to remove the composite primary key from discussion. (If this model
 had more fields, this values query would be more realistic):
 {{{#!py
 class ForeignObjectValues(TestCase):
     def test_foreign_object_values(self):
         from .models import Customer
         customer_1 = Customer.objects.create(customer_id=1, company="a")
         customer_2 = Customer.objects.create(customer_id=1, company="b")
         company_a_customers =
 Customer.objects.filter(company="a").values("address", "company")
         self.assertSequenceEqual(
             Customer.objects.exclude(company__in=[cust["company"] for cust
 in company_a_customers]),
             [customer_2],  # Fails, all companies included
         )
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36431>
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/0107019735ce4c80-86bb4ff7-ac71-4fe6-955f-8ce788623e3b-000000%40eu-central-1.amazonses.com.

Reply via email to