-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Activerecord DISTINCT ON for json data type in postgresql #17706
Comments
Can confirm that. I am having the exact same issue right now. It works fine if one uses I guess it would be nice if one could specify columns like: |
Same issue here with rails 4.2 & postgres 9.4.0 I however cannot seem to replace my uniq with a select distinct on, maybe my syntax is wrong? Before adding a JSON datatype column to my Product model this worked:
This is what i did
|
I've also been able to reproduce the issue. I'm currently testing a fix using the idea by naxnam to have a distinct method that takes in the name of the column as well as true and false values. Events.distinct(:id) should work as well as Events.distinct(true). |
FWIW, I think that needing to write |
Unfortunately with DISTINCT ON, all I have a JSON field in a PostGIS-enabled 9.3 database table:
Error: Fine, let's do this manually: Success. Sort of. Now, in a contrived example, let's do a fancy ORDER BY expression. In PostGIS, I can find "nearby" objects in the real world based on spatial data: Model.where(:type=>1).select("DISTINCT ON (model.id) model.*").limit(1)
.order("multi_bounds::geometry <#> st_setsrid(st_makepoint(#{query["longitude"]}, #{query["latitude"]}), 4326)") With error: (excuse me for omitting in the sample, but I already cast lat/lon data to float, so SQL-i is safe here) Okay, so, the documentation for |
@sgrif there is a problem though, what if you are performing an Edit: Nevermind, looks like it won't collide |
Fix: Source: activerecord-hackery/ransack#453 |
Yeah, unfortunately Postgres |
In case anyone else runs across this issue and, for whatever reason, would prefer to use |
Should this issue be closed now if using |
FWIW, our shop has hit this and we are tied to Postgres 9.3 for a while yet. It is an annoying problem because the addition of a JSON field to, in this case, a |
@gltarsa this isn't something that can be fixed by Rails. If anything, you should report it as a bug in the JSON data type in Postgres. @matthewd this ticket should be closed. |
👍 for adding I am using PG > 9.3 but cannot migrate to jsonb because some legacy business logic relies on the order of the keys (which json guarantees but jsonb does not). Please help |
@calin-ciobanu you could coerce the JSON into JSONB during the query: # distinct on the entire JSON object
Model.select 'distinct on (data::jsonb) *'
# distinct on a key in the JSON
Model.select "distinct on (data::jsonb->'id') *" Though that wouldn't work if you need JSON objects with different key orders to be considered distinct. |
Any news on this, or workarounds besides the awkward |
The same problem exists if there's point type column in the table. I've made #41622 to introduce |
Why does Rails not use |
@hrdwdmrbl |
@machty Sure, that's fine, but then shouldn't the Postgres adapter be doing that? You know, the quirks of different implementations is the reason for adapters. In the specific case of Postgres, the ActiveRecord API is just not working at all the way we would expect it to. |
@hrdwdmrbl tbh I don't think I have enough expertise to think through every conceivable use case / corner case to be certain that |
@machty Just to clarify a minor point, I think it should be |
When a field on postgres is json type, and you make a call to uniq or distinct, the query generates SELECT DISTINCT but in order to work with json type on postgres it should be SELECT DISTINCT ON (), Also you can check more details here
The text was updated successfully, but these errors were encountered: