Opened 10 hours ago

Last modified 10 hours ago

#36120 new Bug

Calling .update() on the virtual "pk" field for composite primary keys raises AttributeError — at Version 1

Reported by: Jacob Walls Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Walls)

Do we want to support .update(pk=... for composite primary keys?

If not, let's raise a friendly error.

Rough test for demo purposes:

  • tests/composite_pk/test_update.py

    diff --git a/tests/composite_pk/test_update.py b/tests/composite_pk/test_update.py
    index ec770230fc..6164e0de90 100644
    a b (this hunk was shorter than expected)  
    11from django.db import connection
    22from django.test import TestCase
    33
    44from .models import Comment, Tenant, TimeStamped, Token, User
    class CompositePKUpdateTests(TestCase):  
    175176
    176177        with self.assertRaisesMessage(ValueError, msg):
    177178            Comment.objects.update(user=User())
     179
     180    def test_update_lhs_pk(self):
     181        qs = Comment.objects.filter(user__email=self.user_1.email)
     182        qs.update(pk=(1, 100))  # supported?

======================================================================
ERROR: test_update_lhs_pk (composite_pk.test_update.CompositePKUpdateTests.test_update_lhs_pk)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/source/django/tests/composite_pk/test_update.py", line 182, in test_update_lhs_pk
    qs.update(pk=(1, 100))  # decide whether to support this or not
    ~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/source/django/django/db/models/query.py", line 1254, in update
    rows = query.get_compiler(self.db).execute_sql(ROW_COUNT)
  File "/Users/source/django/django/db/models/sql/compiler.py", line 2068, in execute_sql
    row_count = super().execute_sql(result_type)
  File "/Users/source/django/django/db/models/sql/compiler.py", line 1610, in execute_sql
    sql, params = self.as_sql()
                  ~~~~~~~~~~~^^
  File "/Users/source/django/django/db/models/sql/compiler.py", line 2044, in as_sql
    values.append("%s = %s" % (qn(name), placeholder))
                               ~~^^^^^^
  File "/Users/source/django/django/db/models/sql/compiler.py", line 568, in quote_name_unless_alias
    r = self.connection.ops.quote_name(name)
  File "/Users/source/django/django/db/backends/sqlite3/operations.py", line 190, in quote_name
    if name.startswith('"') and name.endswith('"'):
       ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'

----------------------------------------------------------------------

Change History (1)

comment:1 by Jacob Walls, 10 hours ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top