﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36370	Model.objects.get() with a None char parameter intermittently doesn't retrieve row correctly	Marco Antonio Mauro		"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."	Bug	closed	Database layer (models, ORM)	5.2	Normal	invalid		Marco Antonio Mauro	Unreviewed	0	0	0	0	0	0
