﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33358	Adding duration field with default <24h raises an error on Oracle.	Marcello Dalponte	Marcello Dalponte	"
== BUG DESCRIPTION
When adding a DurationField with a default which is smaller than 24h

{{{#!python
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
{{{#!sql
  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`
"	Bug	closed	Database layer (models, ORM)	3.1	Normal	fixed	durationfield,oracle		Ready for checkin	1	0	0	0	0	0
