django_polymorphic icon indicating copy to clipboard operation
django_polymorphic copied to clipboard

"result objects do not have unique primary keys" error when fetching randomly-sorted queryset

Open tuatara opened this issue 14 years ago • 1 comments

I've had an intermittent 500 error, with this report:

AssertionError: django_polymorphic: result objects do not have unique primary keys - model <class 'mash.articles.models.Story'>

It's always been on that model, which led me to a query in a template tag that returns a random story:

stories = Story.objects.filter(suppress=False, publish_date__isnull=False, publish_date__gt=datetime.now(), byline__isnull=False).order_by('?')[:5]

If I execute that query many times in the Django shell, I eventually get that error. If I run it with the order_by clause removed, or changed to order on a field, it works reliably. It also worked reliably with normal model inheritance. So I'm thinking there's something about the way django-polymorphic treats random ordering, but a quick reading of the code doesn't flag anything for me.

This is with Postgres 8.4. Happy to share models if it would help.

Thanks Matt

tuatara avatar Apr 28 '11 03:04 tuatara

I too have this problem, to reproduce it by running this query, Article.objects.filter(tags__slug__in=['tag1', 'tag2']) if an article has been tagged by 'tag1' and 'tag2' will result above error

I guess it's the way that query has been customized to work with polymorphic, which results multiple same instance in a result set.

variable avatar Jun 11 '13 02:06 variable