I’m using GitHub - will/crystal-pg: a postgres driver for crystal driver to use PostgreSQL and I have a really annoying error that I have no idea how to fix.
I have a column called watched
which is a just an array of jsonb
(_jsonb
) and I’m trying to retrieve the data from that Array.
For example. I have this:
require "db"
require "pg"
PG_DB = DB.open "postgres://asd:asd@/asd"
request = <<-SQL
select watched
from users
where email = $1
SQL
asd = PG_DB.query_all(request, "user", as:JSON::PullParser)
pp asd
It returns this on compilation:
Unhandled exception: In PG::ResultSet#read the column watched returned a Array(PG::StringArray) but a JSON::PullParser was expected. (DB::ColumnTypeMismatchError)
If I do this:
require "db"
require "pg"
PG_DB = DB.open "postgres://asd:asd@/asd"
request = <<-SQL
select watched
from users
where email = $1
SQL
asd = PG_DB.query_all(request, "user", as:Array(JSON::PullParser))
pp asd
I gives me:
Showing last frame. Use --error-trace for full trace.
In lib/pg/src/pg/decoders/array_decoder.cr:72:31
72 | decoder = array_decoder(T)
^
Error: expected argument #1 to 'PG::Decoders.array_decoder' to be (Bool | Nil).class, (Char | Nil).class, (Float32 | Nil).class, (Float64 | Nil).class, (Int16 | Nil).class, (Int32 | Nil).class, (Int64 | Nil).class, (PG::Numeric | Nil).class, (String | Nil).class, (Time | Nil).class, (UUID | Nil).class, Array(T).class, Bool.class, Char.class, Float32.class, Float64.class, Int16.class, Int32.class, Int64.class, PG::Numeric.class, String.class, Time.class or UUID.class, not JSON::PullParser.class
How I can make this work? Here is a related issue: Error: can't cast to JSON::Any · Issue #263 · will/crystal-pg · GitHub