Skip to content

Commit

Permalink
Fixed empty strings + to_field regression on Oracle
Browse files Browse the repository at this point in the history
Querying the reverse side of nullable to_field relation, where both
sides can contain null values resulted in incorrect results. The reason
was not detecting '' as NULL.

Refs #17541
  • Loading branch information
akaariai committed Feb 22, 2013
1 parent 60fff6f commit 09fcb70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions django/db/models/fields/related.py
@@ -1,6 +1,6 @@
from operator import attrgetter from operator import attrgetter


from django.db import connection, router from django.db import connection, connections, router
from django.db.backends import util from django.db.backends import util
from django.db.models import signals, get_model from django.db.models import signals, get_model
from django.db.models.fields import (AutoField, Field, IntegerField, from django.db.models.fields import (AutoField, Field, IntegerField,
Expand Down Expand Up @@ -496,7 +496,8 @@ def get_query_set(self):
except (AttributeError, KeyError): except (AttributeError, KeyError):
db = self._db or router.db_for_read(self.model, instance=self.instance) db = self._db or router.db_for_read(self.model, instance=self.instance)
qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters) qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters)
if getattr(self.instance, attname) is None: val = getattr(self.instance, attname)
if val is None or val == '' and connections[db].features.interprets_empty_strings_as_nulls:
return qs.none() return qs.none()
qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}} qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}}
return qs return qs
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/many_to_one_regress/tests.py
Expand Up @@ -126,7 +126,7 @@ def test_relation_unsaved(self):
# Now the model is saved, so we will need to execute an query. # Now the model is saved, so we will need to execute an query.
with self.assertNumQueries(1): with self.assertNumQueries(1):
self.assertEqual(th.child_set.count(), 0) self.assertEqual(th.child_set.count(), 0)

def test_related_null_to_field(self): def test_related_null_to_field(self):
c1 = Car.objects.create() c1 = Car.objects.create()
c2 = Car.objects.create() c2 = Car.objects.create()
Expand Down

0 comments on commit 09fcb70

Please sign in to comment.