﻿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
29794	Duplicate object returned using filter	Lars Solberg	nobody	"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"	Bug	closed	Database layer (models, ORM)	2.1	Normal	invalid	duplicate, vacuum		Unreviewed	0	0	0	0	0	0
