Skip to content

Commit

Permalink
Fixed #34936 -- Fixed migration crash for DecimalField with db_defaul…
Browse files Browse the repository at this point in the history
…t on SQLite.

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

Regression in 7414704.
  • Loading branch information
shangxiao authored and felixxm committed Nov 2, 2023
1 parent 1944f49 commit 797957f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions django/db/backends/oracle/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"Oracle doesn't support comparing NCLOB to NUMBER.": {
"generic_relations_regress.tests.GenericRelationTests.test_textlink_filter",
},
"DecimalField.db_default doesn't return decimal.Decimal instances on Oracle "
"(#34941).": {
"field_defaults.tests.DefaultTests.test_field_db_defaults_returning",
},
}
django_test_expected_failures = {
# A bug in Django/oracledb with respect to string handling (#23843).
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def as_sqlite(self, compiler, connection, **extra_context):
sql, params = self.as_sql(compiler, connection, **extra_context)
try:
if self.output_field.get_internal_type() == "DecimalField":
sql = "CAST(%s AS NUMERIC)" % sql
sql = "(CAST(%s AS NUMERIC))" % sql
except FieldError:
pass
return sql, params
Expand Down
4 changes: 4 additions & 0 deletions tests/field_defaults/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

from datetime import datetime
from decimal import Decimal

from django.db import models
from django.db.models.functions import Coalesce, ExtractYear, Now, Pi
Expand All @@ -33,6 +34,9 @@ class DBArticle(models.Model):

headline = models.CharField(max_length=100, db_default="Default headline")
pub_date = models.DateTimeField(db_default=Now())
cost = models.DecimalField(
max_digits=3, decimal_places=2, db_default=Decimal("3.33")
)

class Meta:
required_db_features = {"supports_expression_defaults"}
Expand Down
9 changes: 6 additions & 3 deletions tests/field_defaults/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from decimal import Decimal
from math import pi

from django.db import connection
Expand Down Expand Up @@ -44,6 +45,7 @@ def test_field_db_defaults_returning(self):
self.assertIsInstance(a.id, int)
self.assertEqual(a.headline, "Default headline")
self.assertIsInstance(a.pub_date, datetime)
self.assertEqual(a.cost, Decimal("3.33"))

@skipIfDBFeature("can_return_columns_from_insert")
@skipUnlessDBFeature("supports_expression_defaults")
Expand All @@ -54,6 +56,7 @@ def test_field_db_defaults_refresh(self):
self.assertIsInstance(a.id, int)
self.assertEqual(a.headline, "Default headline")
self.assertIsInstance(a.pub_date, datetime)
self.assertEqual(a.cost, Decimal("3.33"))

def test_null_db_default(self):
obj1 = DBDefaults.objects.create()
Expand Down Expand Up @@ -141,12 +144,12 @@ def test_bulk_create_all_db_defaults_one_field(self):
articles = [DBArticle(pub_date=pub_date), DBArticle(pub_date=pub_date)]
DBArticle.objects.bulk_create(articles)

headlines = DBArticle.objects.values_list("headline", "pub_date")
headlines = DBArticle.objects.values_list("headline", "pub_date", "cost")
self.assertSequenceEqual(
headlines,
[
("Default headline", pub_date),
("Default headline", pub_date),
("Default headline", pub_date, Decimal("3.33")),
("Default headline", pub_date, Decimal("3.33")),
],
)

Expand Down

0 comments on commit 797957f

Please sign in to comment.