question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems withs PostGres 9.3, JSON Field and Query Distinct

See original GitHub issue

As mentionned in #47, using “.distinct()”, which is used by the admin panel creating users but also in several other cases in django processes, triggers the same bug as #47 describes.

A workaround has been found by @mkhattab : The work around for this in the Django admin is to subclass QuerySet and Manager and override the distinct method [to remove the JSON fields from it].

What would be the definite fix ?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
srusskihcommented, Mar 17, 2014

Solved my case.

Model.some_query_here.order_by('id').distinct('id')

Model.some_query_here.order_by('-views_count', 'id').distinct('views_count', 'id')

Hope, this helps to somebody

1reaction
kot-behemothcommented, Jan 21, 2014

I had this issue popping up in Admin list views for models that contained JSON columns (even though I wasn’t showing those field in the view). Interestingly enough, when the view is called, it doesn’t actually call the distinct method on the Manager. My workaround was to subclass the manager, and override the get_query_set method as follows:

def get_queryset(self):
    """
    Returns a new QuerySet object which excludes JSON `key_values` column.
    """
    return QuerySet(self.model, using=self._db).defer('key_values')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems withs PostGres 9.3, JSON Field and Query Distinct
I tried running the OrderedDictSerializationTest class which came in the out-of-the-box tests.py for 0.9.20. I'm using Postgres 9.3.2 and Django 1.5. The class ......
Read more >
Is it possible to select distinct values in a json document using ...
Assuming a JSON array in a Postgres 9.4 jsonb column, this would do the job: SELECT DISTINCT ON (doc->'s') doc FROM ( SELECT...
Read more >
Distinct on Postgresql JSON data column - Stack Overflow
The reason behind this, is that in PostgreSQL (up to 9.3) there is no equality operator defined for json (i.e. val1::json = val2::json...
Read more >
9.4: JSON Functions and Operators - PostgreSQL
Many of these operators can be indexed by jsonb operator classes. ... This form of json_object takes keys and values pairwise from two...
Read more >
Select distinct values by key from JSONB column-postgresql
how to get data from jsonb column in postgres? · PostgreSQL - How to convert current timestamp into bytea · PostgreSQL & DataMapper...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found