Refs #31403 -- Made SQLInsertCompiler.execute_sql() always return an iterable of rows.

List of tuples.
This commit is contained in:
Adam Johnson 2020-03-26 11:31:02 +00:00 committed by Mariusz Felisiak
parent 00ff7a44de
commit c06d7c9239
4 changed files with 11 additions and 12 deletions

View File

@ -253,6 +253,7 @@ END;
return " DEFERRABLE INITIALLY DEFERRED"
def fetch_returned_insert_columns(self, cursor, returning_params):
columns = []
for param in returning_params:
value = param.get_value()
if value is None or value == []:
@ -264,7 +265,8 @@ END;
'https://code.djangoproject.com/ticket/28859).'
)
# cx_Oracle < 7 returns value, >= 7 returns list with single value.
yield value[0] if isinstance(value, list) else value
columns.append(value[0] if isinstance(value, list) else value)
return tuple(columns)
def field_cast_sql(self, db_type, internal_type):
if db_type and db_type.endswith('LOB'):

View File

@ -889,8 +889,9 @@ class Model(metaclass=ModelBase):
returning_fields = meta.db_returning_fields
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
for result, field in zip(results, returning_fields):
setattr(self, field.attname, result)
if results:
for value, field in zip(results[0], returning_fields):
setattr(self, field.attname, value)
return updated
def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):

View File

@ -1249,15 +1249,11 @@ class QuerySet:
bulk_return = connections[self.db].features.can_return_rows_from_bulk_insert
for item in [objs[i:i + batch_size] for i in range(0, len(objs), batch_size)]:
if bulk_return and not ignore_conflicts:
inserted_columns = self._insert(
inserted_rows.extend(self._insert(
item, fields=fields, using=self.db,
returning_fields=self.model._meta.db_returning_fields,
ignore_conflicts=ignore_conflicts,
)
if isinstance(inserted_columns, list):
inserted_rows.extend(inserted_columns)
else:
inserted_rows.append(inserted_columns)
))
else:
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
return inserted_rows

View File

@ -1385,10 +1385,10 @@ class SQLInsertCompiler(SQLCompiler):
return self.connection.ops.fetch_returned_insert_rows(cursor)
if self.connection.features.can_return_columns_from_insert:
assert len(self.query.objs) == 1
return self.connection.ops.fetch_returned_insert_columns(cursor, self.returning_params)
return [self.connection.ops.last_insert_id(
return [self.connection.ops.fetch_returned_insert_columns(cursor, self.returning_params)]
return [(self.connection.ops.last_insert_id(
cursor, self.query.get_meta().db_table, self.query.get_meta().pk.column
)]
),)]
class SQLDeleteCompiler(SQLCompiler):