PRIMARY KEY in view PostgreSQL

15 views
Skip to first unread message

Matthew Pava

unread,
Dec 11, 2017, 10:43:34 AM12/11/17
to django...@googlegroups.com

Fellow users,

I have an unmanaged model that references a view in my PostgreSQL 9.5 backend.  I am trying to apply an annotation through the ORM in that model’s manager.  Unfortunately, I am getting an error that some fields “must appear in the GROUP BY clause or be used in an aggregate function.”

 

Upon further investigation, it appears that I need a PRIMARY KEY in the backend to avoid having to include columns in a GROUP BY clause, but PostgreSQL does not allow for such constraints to be added to a VIEW.  The “id” column is just a row_number().

 

I was wondering how anyone else handled this issue.

Thanks!

Simon Charette

unread,
Dec 11, 2017, 2:35:11 PM12/11/17
to Django users
Hello Matthew,

This should be fixed in Django 2.0 by daf2bd3efe53cbfc1c9fd00222b8315708023792[0].

I'd appreciate if you could chime in the related django-developer thread[1] to mention
you were using an unmanaged model to query views as there was no consensus
regarding whether or not the patch should be rolled back.

I think you're a great example of a user who hit this issue from starting to use Django
< 2.0 that is not taken into account by one of the user argument to favor rolling back
the patch[2].

The more I think of it the more I feel like we should just close the ticket until the
ORM gains enough introspection power to determine whether or not we should be
using the optimization on unmanaged model.

Here's the Trac ticket for reference[2].

Cheers,
Simon

[0] https://github.com/django/django/commit/daf2bd3efe53cbfc1c9fd00222b8315708023792
[1] https://groups.google.com/forum/#!topic/django-developers/lx3ZSq-W9X4
[2| https://groups.google.com/d/msg/django-developers/lx3ZSq-W9X4/yh4I2CsoBwAJ
[3] https://code.djangoproject.com/ticket/28107

Matthew Pava

unread,
Dec 11, 2017, 3:46:06 PM12/11/17
to django...@googlegroups.com

Thank you so much, Simon, for that response!  I do wish I could use Django 2.0, but some of my third-party apps haven’t been updated yet.  I’m using Django 1.11.

 

With the clues provided, I was able to test my query by adding all of the columns into the GROUP BY clause in my PostgreSQL SQL editor.  In the process, I learned that my query takes way too long to run (about 5 minutes).

 

As you requested, I added a message to the django-developers mailing list on that particular thread.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f89fa330-0c6b-43eb-ae86-7926b67669ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages