#36189: Oracle Backend with `"use_returning_into": False` Option Fails to Retrieve
Last Inserted ID
-------------------------------------+-------------------------------------
Reporter: ybjeon01 | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
When running tests in `django-docker-box` with Oracle, setting the
`"use_returning_into": False` option in `settings.py` prevents Django from
retrieving the last inserted ID. The issue can be reproduced by modifying
the database options as shown below:
{{{
if engine.endswith(".oracle"):
entry |= {
"NAME": name,
"OPTIONS": {
"use_returning_into": False,
}
}
...
}}}
**Cause of the Issue**
The issue originates from the `last_insert_id()` method in
`django/db/backends/oracle/operations.py`. The SQL executed in this method
does not properly handle the query format, leading to a failure in
retrieving the sequence value.
**Current Implementation (Buggy Code)**
{{{
def last_insert_id(self, cursor, table_name, pk_name):
sq_name = self._get_sequence_name(cursor, strip_quotes(table_name),
pk_name)
cursor.execute('"%s".currval' % sq_name)
return cursor.fetchone()[0]
}}}
**Proposed Fix**
The issue can be resolved by modifying the query to include the correct
SQL syntax:
{{{
def last_insert_id(self, cursor, table_name, pk_name):
sq_name = self._get_sequence_name(cursor, strip_quotes(table_name),
pk_name)
template = 'SELECT "%s".currval' +
self.connection.features.bare_select_suffix
cursor.execute(template % sq_name)
return cursor.fetchone()[0]
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36189>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.