Calling .update() on the virtual "pk" field for composite primary keys raises AttributeError
Do we want to support .update(pk=...
for composite primary keys?
If not, let's raise a friendly error.
Rough test for demo purposes:
======================================================================
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
(8)
Description: |
modified (diff)
|
Description: |
modified (diff)
|
Triage Stage: |
Unreviewed → Accepted
|
Has patch: |
set
|
Owner: |
set to Jacob Walls
|
Status: |
new → assigned
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
Likely easier to raise here than to adapt
SQLUpdateCompiler
. A good spot to intercept composite primary key assignment is likelyUpdateQuery.add_update_values
which already intercepts relational and many-to-many field updates.