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

Use element operator instead of contains for jsonb querying #159

Merged
merged 3 commits into from
Jan 28, 2018

Conversation

shioyama
Copy link
Owner

Currently, queries on jsonb columns in the Jsonb and Container backends are performed using the "contains" operator (@>). This works fine for string/integer/boolean values, but for hash valued-translations it returns a match even if the hash contains other keys/values (i.e. it does not perform an exact match).

Instead of using "contains" (@>), we can also use the normal element operator (->), so for a query Post.i18n.where(title: { "a" => "b" }), instead of this:

SELECT "posts".* FROM "posts" WHERE ("posts"."title" @> '{"en":{"a":"b"}}')

we instead get this:

SELECT "posts".* FROM "posts" WHERE (("posts"."title" -> 'en') = '{"a":"b"}')

The first SQL will match a post with a title value '{"a":"b","c":"d"}', but the second will not.

Although this is an edge case, I think in general it's also better not to use the jsonb-specific contains operator if it's not really necessary anyway.

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

Successfully merging this pull request may close these issues.

1 participant