diff options
| author | DevilsAutumn <bhuvnesh875@gmail.com> | 2022-11-22 15:04:55 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-11-22 20:04:38 +0100 |
| commit | 170322451acc18ff3311c68db1b9b1d3f7cb7913 (patch) | |
| tree | c9ae3bd09cb7a101bd84bb0ab921ec109052d629 /django | |
| parent | 3b0a8ea2993a2b011ad57304b3f978947adb5981 (diff) | |
[4.1.x] Fixed #34171 -- Fixed QuerySet.bulk_create() on fields with db_column in unique_fields/update_fields.
Bug in 0f6946495a8ec955b471ca1baaf408ceb53d4796.
Thanks Joshua Brooks for the report.
Backport of 4035bab56f2862a25cd7bfba41a84e58672cb1cc from main
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 9 | ||||
| -rw-r--r-- | django/db/models/sql/compiler.py | 4 |
2 files changed, 6 insertions, 7 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index cd14b3a67e..c7485b87fb 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -716,7 +716,6 @@ class QuerySet: "Unique fields that can trigger the upsert must be provided." ) # Updating primary keys and non-concrete fields is forbidden. - update_fields = [self.model._meta.get_field(name) for name in update_fields] if any(not f.concrete or f.many_to_many for f in update_fields): raise ValueError( "bulk_create() can only be used with concrete fields in " @@ -728,9 +727,6 @@ class QuerySet: "update_fields." ) if unique_fields: - unique_fields = [ - self.model._meta.get_field(name) for name in unique_fields - ] if any(not f.concrete or f.many_to_many for f in unique_fields): raise ValueError( "bulk_create() can only be used with concrete fields " @@ -782,8 +778,11 @@ class QuerySet: if unique_fields: # Primary key is allowed in unique_fields. unique_fields = [ - opts.pk.name if name == "pk" else name for name in unique_fields + self.model._meta.get_field(opts.pk.name if name == "pk" else name) + for name in unique_fields ] + if update_fields: + update_fields = [self.model._meta.get_field(name) for name in update_fields] on_conflict = self._check_bulk_create_options( ignore_conflicts, update_conflicts, diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index ae095170f8..ed03c7b186 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -1607,8 +1607,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 |
