[Django] #36370: Model.objects.get() with a None char parameter intermittently doesn't retrieve row correctly

7 views
Skip to first unread message

Django

unread,
May 6, 2025, 9:18:45 AMMay 6
to django-...@googlegroups.com
#36370: Model.objects.get() with a None char parameter intermittently doesn't
retrieve row correctly
-------------------------------------+-------------------------------------
Reporter: marcus905 | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
With the below Models:
{{{
class Bar(models.Model):
id = models.BigAutoField(primary_key=True)
origin = models.CharField(max_length=1, blank=True, null=True)


class Foo(models.Model):
id = models.OneToOneField('Bar', models.DO_NOTHING, db_column='id',
primary_key=True)
date = models.DateField()
origin = models.CharField(max_length=1, blank=True, null=True)
extra_data = models.JSONField(blank=True, null=True)
}}}
executing this:
{{{
bar, _ = Bar.objects.get_or_create(
origin = 'a',
)
origin = None # may be a str with something in, an empty str or
None

try:
foo = Foo.objects.get(id=bar, origin=origin)
except Foo.DoesNotExist:
foo = Foo(
id=bar,
origin=origin,
)
# ...
foo.save()

}}}

If we have None there in origin, the exception apparently gets thrown
intermittently even if there is a row present in the DB and therefore it
tries creating a new row, while if I have an empty string in the origin
field, the get() call works as expected.

I managed to trigger this bug relatively consistently if I try operating
on the same row at in a loop at least 3 times, so if the snippet gets
executed 3 times, the 3rd time it'll try to create the row instead of
retrieving and editing the already present one.

Used DB is Postgres with the GeoDjango and PostGIS in use, but tables are
normal ones without geography.
--
Ticket URL: <https://code.djangoproject.com/ticket/36370>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 6, 2025, 2:31:29 PMMay 6
to django-...@googlegroups.com
#36370: Model.objects.get() with a None char parameter intermittently doesn't
retrieve row correctly
-------------------------------------+-------------------------------------
Reporter: Marco Antonio Mauro | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: => invalid
* status: new => closed

Comment:

Hello Marco Antonio Mauro, thank you for taking the time to create this
report. I have simplified your example and created a test to try to
reproduce what you are getting:
{{{#!python
class Bar(models.Model):
id = models.BigAutoField(primary_key=True)
origin = models.CharField(max_length=1, blank=True, null=True)


class Foo(models.Model):
id = models.OneToOneField('Bar', models.DO_NOTHING, db_column='id',
primary_key=True)
origin = models.CharField(max_length=1, blank=True, null=True)
}}}
The following test never fails for me (the `assertIsNotNone` could be
removed):
{{{#!python
class Ticket36370TestCase(TestCase):

def test_try(self):
from .models import Bar, Foo
bar, _ = Bar.objects.get_or_create(origin='a')
origin = None # may be a str with something in, an empty str or
None

for i in range(10):
with self.subTest(i=i):
try:
foo = Foo.objects.get(id=bar, origin=origin)
except Foo.DoesNotExist:
foo = Foo(id=bar, origin=origin)

foo.save()
self.assertIsNotNone(foo)
foo_db = Foo.objects.get(id=bar, origin=origin)
self.assertIsNotNone(foo_db)
}}}

I'm guessing you may have other code or dependency in place that may
affect your runs? Are you using threads or some form of concurrent
execution?

Because of the above, I think this report seems better suited to be a
support request. The best place to get answers to your issue is using any
of the user support channels from
[https://docs.djangoproject.com/en/dev/faq/help/#how-do-i-do-x-why-
doesn-t-y-work-where-can-i-go-to-get-help this link].

Since the goal of this issue tracker is to track issues about Django
itself, and your issue seems, at first, to be located in your custom code,
I'll be closing this ticket as invalid following the
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#closing-tickets ticket triaging process]. If, after debugging,
you find out that this is indeed a bug in Django, please re-open with the
specific details and please be sure to include a small Django project to
reproduce or a failing test case.
--
Ticket URL: <https://code.djangoproject.com/ticket/36370#comment:1>
Reply all
Reply to author
Forward
0 new messages