Opened 4 years ago
Closed 4 years 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:
- the duration is not enclosed in quotes
0:05:00->'0:05:00' - 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 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 4 years ago
| Has patch: | set |
|---|
comment:3 by , 4 years ago
| Needs tests: | set |
|---|---|
| Summary: | Adding duration field with default <24h raises an error in oracle → Adding duration field with default <24h raises an error on Oracle. |
| Triage Stage: | Unreviewed → Accepted |
comment:4 by , 4 years ago
| Needs tests: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
Here is the fix https://github.com/django/django/pull/15183