#29794: Duplicate object returned using filter
-------------------------------------+-------------------------------------
Reporter: xeor | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords: duplicate, vacuum
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Note: I have am unable to reproduce this issue, it showed up in my prod-
environment, but only with a specific db-entry. I think it's a bug, but I
need help reproducing it..
Note 2: This problem fixed itself doing a database vacuum.. I tried that
manually after I wrote the whole issue. Still think the issue is weird,
maybe someone have something useful to say. At least keeping the issue for
searchability for the next person hitting this. Maybe there is a way for
django to detect cases like this?
Using a simple model for explaination:
{{{
class LowercaseCharField(models.CharField):
def get_prep_value(self, value):
return str(value).lower()
class Server(models.Model):
objects = ServerBaseManager.from_queryset(ServerQuerySet)()
name = LowercaseCharField(max_length=255)
domain = models.ForeignKey(Domain, db_index=True,
default=default_domain, on_delete=models.SET_DEFAULT)
class Meta:
unique_together = (
('name', 'domain')
)
}}}
ServerBaseManager and ServerQuerySet does not override any functions, just
add functionality.
The domain ForeignKey is nothing magical either..
On 1 specific name entry, lets call it '''server1''' there are something
strange happening.
There are 2 entries with the '''server1''' name in the db, one with
'''domain.pk=1''', and one with '''domain.pk=2'''
Here is the strange part:
{{{#!python
In [1]: [i.pk for i in Server.objects.filter(name='server1')]
Out[1]: [1, 2]
In [2]: Server.objects.filter(name='server1')[0].pk
Out[2]: 2
In [3]: Server.objects.filter(name='server1')[1].pk
Out[3]: 2
In [4]: Server.objects.filter(name='server1').order_by('pk')[0].pk
Out[4]: 1
In [5]: Server.objects.filter(name='server1').order_by('pk')[1].pk
Out[5]: 2
In [6]: Server.objects.filter(name='server1').order_by('-name')[0].pk
Out[6]: 2
In [7]: Server.objects.filter(name='server1').order_by('-name')[1].pk
Out[7]: 2
In [8]: Server.objects.filter(name='server1').order_by('name')[0].pk
Out[8]: 2
In [9]: Server.objects.filter(name='server1').order_by('name')[1].pk
Out[9]: 2
In [10]: Server.objects.filter(name='server1').values('pk')
Out[10]: <ServerQuerySet [{'pk': 1}, {'pk': 2}]>
In [11]:
Server.objects.filter(pk__in=Server.objects.filter(name='server1'))[0].pk
Out[11]: 1
In [12]:
Server.objects.filter(pk__in=Server.objects.filter(name='server1'))[1].pk
Out[12]: 2
}}}
as you can see. The queryset returns the same object if I access it using
'''queryset[0]''', or '''queryset[1]''', but in a lot of other cases, it
works as it should.
* '''queryset.query''' returns nothing magical.. Just a simple SELECT
query
* I have plenty of duplicate names, but this only happens to server1, tho
this is hard to test in bulk, since the duplicate problem won't show up if
I try to automate the testing..
versions:
* python: 3.6.5
* postgres 9.4
* django: 2.1.1
--
Ticket URL: <https://code.djangoproject.com/ticket/29794>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.fe47b2b8e6d04e51274ee8ee320f56d0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.