#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070195085b42d7-108ce14b-9434-4181-bf93-07c63c819abf-000000%40eu-central-1.amazonses.com.