Andy Jones Andy Jones - 6 months ago 59
Ruby Question

pg gem: 'Warning: no type cast defined for type "numeric" '

I'm having trouble getting typed results out of the pg gem.

require 'pg'
require_relative 'spec/fixtures/database'

client = PG.connect( DB[:pg] )
client.type_map_for_queries = PG::BasicTypeMapForQueries.new(client)
client.type_map_for_results = PG::BasicTypeMapForResults.new(client)

client.exec( %|select * from testme;| ) do |query|
query.each {|r| puts r.inspect }
end


This program gives the output:

Warning: no type cast defined for type "money" with oid 790. Please cast this type explicitly to TEXT to be safe for future changes.
Warning: no type cast defined for type "numeric" with oid 1700. Please cast this type explicitly to TEXT to be safe for future changes.
{"string"=>"thing", "logical"=>true, "cash"=>"£1.23", "reel"=>"2.34", "day"=>#<Date: 2015-12-31 ((2457388j,0s,0n),+0s,2299161j)>, "float"=>3.45}


So: booleans and floats and dates (and integers) get converted, but not numerics or the money type.

Can anyone tell me how to "cast the type explicitly", assuming that I don't want to hard-code a solution for each table?

Answer

Got the same problem with a text-ish field. Solved by duplicating a coder and editing its OID.

text_coder = client.type_map_for_results.coders.find { |c| c.name == 'text' }
new_coder = text_coder.dup.tap { |c| c.oid = 19 } # oid from the warning
conn.type_map_for_results.add_coder(new_coder)
Comments