Skip to content
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

Error when trying to query a JSONB field that is an array of objects #100

Open
avanrielly opened this issue Feb 29, 2024 · 0 comments
Open

Comments

@avanrielly
Copy link

avanrielly commented Feb 29, 2024

I am not sure if there is something wrong with my setup or if there is an issue with the gem. It seems that I cannot use the contains query against JSONB fields that contain an array of objects. It returns an error trying to parse the parameters. The raw Postgres query works fine.

Raw PostgresSQL query

Button.where("draft_button_fields @> ?", [{required: "always"}].to_json).count
  Button Count (1.7ms)  SELECT COUNT(*) FROM "buttons" WHERE (draft_button_fields @> '[{"required":"always"}]') /*line:(pry):2:in `__pry__'*/
=> 1

It errors with or without the array.
Error:

Button.where.contains(draft_button_fields: [{required: "always"}]).to_sql
TypeError: can't quote Hash
from /Users/avanrielly/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.0.8.1/lib/active_record/connection_adapters/abstract/quoting.rb:25:in `quote'

Table structure:

                                    Table "public.buttons"
         Column         |              Type              | Collation | Nullable |   Default
------------------------+--------------------------------+-----------+----------+-------------
 id                     | bytea                          |           | not null |
...
 draft_button_fields    | jsonb                          |           |          | '[]'::jsonb

Column data:

[{"field_id": "name", "required": "always", ...}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant