I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI
SELECT id FROM program WHERE document -> 'dept' ? 'CS'
When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this
SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}")
.on('dept -> "CS")
....
SQLException: : No value specified for parameter 5. (SimpleParameterList.java:223)
(in my actual queries there are more parameters)
I can get around this by casting my parameter to type jsonb
and using the @>
operator to check containment.
SQL(s"SELECT id FROM program WHERE document -> 'dept' @> {dept}::jsonb")
.on('dept -> "CS")
....
I'm not too keen on the work around. I don't know if there are performance penalties for the cast, but it's extra typing, and non-obvious.
Is there anything else I can do?
SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept} .on('dept -> "CS")
→on
must be apply onSQL(...)
, not be part of the statement string as it seems to be there =SQL("...").on(...)
. Note that string interpolation there is useless. – Defermentapply
after theon
– Brade??
. See postgresql.org/message-id/… – Excellence