summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3
diff options
context:
space:
mode:
authorgirishsontakke <girishsontakke7@gmail.com>2021-04-06 21:50:31 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-04-07 20:09:56 +0200
commit98abc0c90e0eb7fe3a71cfa360962cf59605f1d3 (patch)
tree3e660de54ec94caa11ab8ef14e98f8cbf231abc0 /django/db/backends/sqlite3
parent3f2920ae1d91e67ebf677d407da528c04188384e (diff)
Fixed #32501 -- Added support for returning fields from INSERT statements on SQLite 3.35+.
Diffstat (limited to 'django/db/backends/sqlite3')
-rw-r--r--django/db/backends/sqlite3/features.py6
-rw-r--r--django/db/backends/sqlite3/operations.py19
2 files changed, 25 insertions, 0 deletions
diff --git a/django/db/backends/sqlite3/features.py b/django/db/backends/sqlite3/features.py
index ddae4c8efa..6a7aa09fc9 100644
--- a/django/db/backends/sqlite3/features.py
+++ b/django/db/backends/sqlite3/features.py
@@ -117,3 +117,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
can_introspect_json_field = property(operator.attrgetter('supports_json_field'))
has_json_object_function = property(operator.attrgetter('supports_json_field'))
+
+ @cached_property
+ def can_return_columns_from_insert(self):
+ return Database.sqlite_version_info >= (3, 35)
+
+ can_return_rows_from_bulk_insert = property(operator.attrgetter('can_return_columns_from_insert'))
diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py
index c578979777..95484931cf 100644
--- a/django/db/backends/sqlite3/operations.py
+++ b/django/db/backends/sqlite3/operations.py
@@ -76,6 +76,13 @@ class DatabaseOperations(BaseDatabaseOperations):
"""
return "django_date_extract('%s', %s)" % (lookup_type.lower(), field_name)
+ def fetch_returned_insert_rows(self, cursor):
+ """
+ Given a cursor object that has just performed an INSERT...RETURNING
+ statement into a table, return the list of returned data.
+ """
+ return cursor.fetchall()
+
def format_for_duration_arithmetic(self, sql):
"""Do nothing since formatting is handled in the custom function."""
return sql
@@ -365,3 +372,15 @@ class DatabaseOperations(BaseDatabaseOperations):
def insert_statement(self, ignore_conflicts=False):
return 'INSERT OR IGNORE INTO' if ignore_conflicts else super().insert_statement(ignore_conflicts)
+
+ def return_insert_columns(self, fields):
+ # SQLite < 3.35 doesn't support an INSERT...RETURNING statement.
+ if not fields:
+ return '', ()
+ columns = [
+ '%s.%s' % (
+ self.quote_name(field.model._meta.db_table),
+ self.quote_name(field.column),
+ ) for field in fields
+ ]
+ return 'RETURNING %s' % ', '.join(columns), ()