Opened 13 months ago

Closed 13 months ago

Last modified 10 months ago

#34936 closed Bug (fixed)

db_default with decimal.Decimal() crashes on SQLite.

Reported by: Mariusz Felisiak Owned by: David Sanders
Component: Database layer (models, ORM) Version: 5.0
Severity: Release blocker Keywords:
Cc: Lily Foote 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

db_default with decimal.Decimal() crashes on SQLite. For example, adding the following field

models.DecimalField(null=True, max_digits=5, decimal_places=2, db_default=Decimal("3.33")

generates:

ALTER TABLE "test_adfldd_pony" ADD COLUMN "height" decimal DEFAULT CAST('3.33' AS NUMERIC) NULL;

and crashes with:

django.db.utils.OperationalError: near "(": syntax error

Change History (7)

comment:1 by David Sanders, 13 months ago

Triage Stage: UnreviewedAccepted

Confirmed that SQLite doesn't like CAST(…) in the DEFAULT clause unless wrapped in parens 👍

https://www.sqlite.org/lang_createtable.html#the_default_clause

comment:2 by David Sanders, 13 months ago

Has patch: set
Owner: changed from nobody to David Sanders
Status: newassigned

comment:3 by Mariusz Felisiak, 13 months ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 13 months ago

In 1944f49:

Refs #34936 -- Added test for altering DecimalField with db_default to non-nullable.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 13 months ago

Resolution: fixed
Status: assignedclosed

In 797957fb:

Fixed #34936 -- Fixed migration crash for DecimalField with db_default on SQLite.

CAST() must be wrapped in parentheses to be recognized as an expression on SQLite.

Regression in 7414704e88d73dafbcfbb85f9bc54cb6111439d3.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 13 months ago

In 8dde0504:

[5.0.x] Fixed #34936 -- Fixed migration crash for DecimalField with db_default on SQLite.

CAST() must be wrapped in parentheses to be recognized as an expression on SQLite.

Regression in 7414704e88d73dafbcfbb85f9bc54cb6111439d3.

Backport of 797957fb48f63760442d3d4c8985fd94f8602d5e from main

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 10 months ago

In 06b05c7:

[5.0.x] Refs #34936 -- Added test for altering DecimalField with db_default to non-nullable.

Backport of 1944f490f91e44e1378d53420b64a78163aea815 from main

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