Opened 5 months ago
Closed 5 months ago
#36370 closed Bug (invalid)
Model.objects.get() with a None char parameter intermittently doesn't retrieve row correctly
Reported by: | Marco Antonio Mauro | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.2 |
Severity: | Normal | Keywords: | |
Cc: | Marco Antonio Mauro | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
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:
The following test never fails for me (the
assertIsNotNone
could be removed):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 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 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.