Skip to content

Commit

Permalink
Fixed null handling in Value expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jarshwah authored and timgraham committed Jan 8, 2015
1 parent 1f03d2d commit 8713ea7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ def __init__(self, value, output_field=None):
self.value = value

def as_sql(self, compiler, connection):
if self.value is None:
# cx_Oracle does not always convert None to the appropriate
# NULL type (like in case expressions using numbers), so we
# use a literal SQL NULL
return 'NULL', []
return '%s', [self.value]


Expand Down
7 changes: 7 additions & 0 deletions tests/annotations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ def test_mti_annotations(self):
lambda d: (d.other_name, d.other_chain, d.is_open, d.book_isbn)
)

def test_null_annotation(self):
"""
Test that annotating None onto a model round-trips
"""
book = Book.objects.annotate(no_value=Value(None, output_field=IntegerField())).first()
self.assertIsNone(book.no_value)

def test_column_field_ordering(self):
"""
Test that columns are aligned in the correct order for
Expand Down
15 changes: 14 additions & 1 deletion tests/expressions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.core.exceptions import FieldError
from django.db import connection, transaction, DatabaseError
from django.db.models import F
from django.db.models import F, Value
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import Approximate
from django.utils import six
Expand Down Expand Up @@ -174,6 +174,19 @@ def test_update_with_fk(self):
ordered=False
)

def test_update_with_none(self):
Number.objects.create(integer=1, float=1.0)
Number.objects.create(integer=2)
Number.objects.filter(float__isnull=False).update(float=Value(None))
self.assertQuerysetEqual(
Number.objects.all(), [
None,
None,
],
lambda n: n.float,
ordered=False
)

def test_filter_with_join(self):
# F Expressions can also span joins
Company.objects.update(point_of_contact=F('ceo'))
Expand Down

0 comments on commit 8713ea7

Please sign in to comment.