Opened 7 months ago

Closed 7 months ago

#33358 closed Bug (fixed)

Adding duration field with default <24h raises an error on Oracle.

Reported by: Marcello Dalponte Owned by: Marcello Dalponte
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: durationfield, oracle
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

BUG DESCRIPTION

When adding a DurationField with a default which is smaller than 24h

class Scheduler(models.Model):
    delay = models.DurationField(default=timedelta(minutes=5))

I get this error with the oracle backend

  ORA-01735: invalid ALTER TABLE option

INVESTIGATION

I dug into the issue and I found that the reason why the query errors is because the query generated by the migration app is

  ALTER TABLE "EXAMPLE_SCHEDULER" ADD "DELAY" INTERVAL DAY(9) TO SECOND(6) DEFAULT 0:05:00 NOT NULL;

this query has two issues:

  1. the duration is not enclosed in quotes 0:05:00 -> '0:05:00'
  2. oracle requires the day to be in the value '0:05:00' -> '0 0:05:00'

SUGGESTED FIX

A possible fix would be to ensure we format timedelta into the right format when generating migrations, this means extending DatabaseSchemaEditor.prepare_default to do the formatting before quoting the value, right now prepare_default simply proxies the business logic to quote_value which simply casts the timedelta to string.


ENVIRONMENT

python venv:

Django==3.1
cx_Oracle==8.3.0

oracle version:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Change History (5)

comment:1 Changed 7 months ago by Marcello Dalponte

Owner: changed from nobody to Marcello Dalponte
Status: newassigned

comment:2 Changed 7 months ago by Marcello Dalponte

Has patch: set

comment:3 Changed 7 months ago by Mariusz Felisiak

Needs tests: set
Summary: Adding duration field with default <24h raises an error in oracleAdding duration field with default <24h raises an error on Oracle.
Triage Stage: UnreviewedAccepted

comment:4 Changed 7 months ago by Mariusz Felisiak

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:5 Changed 7 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 3b73f77a:

Fixed #33358 -- Fixed handling timedelta < 1 day in schema operations on Oracle.

Note: See TracTickets for help on using tickets.
Back to Top