#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070196a5bf941d-80cc0fa6-3ed5-4873-8c11-b714dbbdfa8d-000000%40eu-central-1.amazonses.com.

Reply via email to