Conversion of plain PostgreSQL query to Slick query
I have a single table ABC with these (relevant) columns
create table 开发者_如何学JAVAabc
(
transaction_id uuid not null,
store_items jsonb not null,
);
store_items is a Sequence[StoreItem] that looks like this:
{"itemId": "123",
"isAccountSafe": false
},
{"itemId": "456",
"isAccountSafe": true
},
{"itemId": "789",
"isAccountSafe": false
}
I want to query the count of store_items
in abc where isAccountSafe
is false
, in the above example the result would be 2. The tricky part is that I'm not joining multiple tables, I'm joining a single table with one of its columns.
Here's the postgres SQL that I got so far:
select count(transaction_id)
from abc
cross join jsonb_array_elements(store_items) elem
where not (elem->>'isAccountSafe')::boolean
I've been wracking my brain figuring out how to do this in slick. My guess was to first do a query of the store_items first and then do a joinLeft
, something like below, but it's wrong. I don't know how to filter isAccountSafe
that sits inside the jsonb column.
val getStoreItems = abc.map(_.storeItems)
val finalQuery = abc
.joinLeft(getStoreItems)
精彩评论