Re: [Django] #34171: QuerySet.bulk_create() crashes on mixed case columns in unique_fields/update_fields. (was: ON CONFLICT sql does not work with column aliases)

3 views
Skip to first unread message

Django

unread,
Nov 21, 2022, 2:18:46 AM11/21/22
to django-...@googlegroups.com
#34171: QuerySet.bulk_create() crashes on mixed case columns in
unique_fields/update_fields.
-------------------------------------+-------------------------------------
Reporter: joshbrooks | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Release blocker | Resolution:
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 Mariusz Felisiak):

* severity: Normal => Release blocker
* cc: Chih Sean Hsu (added)
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

Thanks for the report! Bug in 0f6946495a8ec955b471ca1baaf408ceb53d4796. We
should use columns instead of field names, e.g.
{{{#!diff
diff --git a/django/db/models/query.py b/django/db/models/query.py
index de49e1c58c..fcf0a0616c 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -798,6 +798,10 @@ class QuerySet(AltersData):
self._prepare_for_bulk_create(objs)
with transaction.atomic(using=self.db, savepoint=False):
objs_with_pk, objs_without_pk = partition(lambda o: o.pk is
None, objs)
+ if update_fields:
+ update_fields = [self.model._meta.get_field(name) for
name in update_fields]
+ if unique_fields:
+ unique_fields = [self.model._meta.get_field(name) for
name in unique_fields]
if objs_with_pk:
returned_columns = self._batched_insert(
objs_with_pk,
diff --git a/django/db/models/sql/compiler.py
b/django/db/models/sql/compiler.py
index 0562a71dd1..caf36382b5 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -1725,8 +1725,8 @@ class SQLInsertCompiler(SQLCompiler):
on_conflict_suffix_sql =
self.connection.ops.on_conflict_suffix_sql(
fields,
self.query.on_conflict,
- self.query.update_fields,
- self.query.unique_fields,
+ (f.column for f in self.query.update_fields),
+ (f.column for f in self.query.unique_fields),
)
if (
self.returning_fields
}}}

Would you like to prepare a patch? (regression tests are required)

--
Ticket URL: <https://code.djangoproject.com/ticket/34171#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages