Ruby pg gem:&x27;警告:没有为“类型”定义类型转换;“数字”';

Ruby pg gem:&x27;警告:没有为“类型”定义类型转换;“数字”';,ruby,postgresql,Ruby,Postgresql,我很难从pg gem中获取键入的结果 require 'pg' require_relative 'spec/fixtures/database' client = PG.connect( DB[:pg] ) client.ty

我很难从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
该程序提供以下输出:

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}
警告:没有为oid 790的“money”类型定义类型转换。请将此类型显式转换为文本,以确保将来的更改安全。
警告:没有为oid 1700的“numeric”类型定义类型转换。请将此类型显式转换为文本,以确保将来的更改安全。
{“string”=>“thing”,“logical”=>true,“cash”=>“1.23英镑”,“revel”=>“2.34英镑”,“day”=>,“float”=>3.45}
所以:布尔值、浮点数和日期(以及整数)可以转换,但不能转换数字或货币类型

假设我不想为每个表硬编码一个解决方案,谁能告诉我如何“显式转换类型”

  • 谷歌搜索错误消息:“警告:没有为类型定义类型强制转换”
  • 你可以找到它的来源
  • 重读这门课,我想从150到214行可以考虑 示例:
    • 寄存器\u类型0,'text',PG::textcoder::String
    • alias\u类型0,'varchar','text'
  • 由于register_type和alias_type是
    PG::BasicTypeRegistry::CoderMap的类方法,因此我将使用它们,看看是否有任何变化:
    
    • PG::BasicTypeRegistry::CoderMap.alias_键入0,'money','text'
    • PG::BasicTypeRegistry::CoderMap.alias_类型0,'numeric','text'
  • 在阅读课堂上的评论时,这些字段和其他一些字段的编码/解码似乎没有实现


    你可以考虑使用一个更高级的ORM类库,比如AVTIVECREORD,它实现了更多的类型(),

    < P>与文本ISE字段有相同的问题。通过复制编码器并编辑其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)
    



    我是如何到达那里的:如果问题相似但不完全相同,下一个人可能会感兴趣

    我在网上读到其他人在谈论
    为搜索结果键入映射,但他们不知道如何定义编码者。因为在我的例子中它是一个文本字段,所以我决定尝试克隆一个现有的文本字段。我知道我可以在Rails应用程序中找到文本预设,所以我打开了一个
    Rails控制台
    并搜索:

    adapter = ActiveRecord::Base.connection
    connection = adapter.instance_variable_get("@connection")
    mapping = connection.type_map_for_results
    cd mapping  # my console of choice is `pry`
    ls          # spotted a likely getter named `coders`
    cd coders   # again
    ls          # spotted getter `name` and setter `oid=`
    
    所以我把代码放在解决方案中。试了一下,成功了

    这并不是很容易找到的,所以我决定退出潜伏者模式并分享它。因此:谢谢你带我进来:)


    []

    劫持此线程,经过一些挖掘,我终于找到了添加自定义解码器/编码器的方法,因此发布了以下示例:

    require 'ipaddr'
    require 'pg'
    
    class InetDecoder < PG::SimpleDecoder
      def decode(string, tuple=nil, field=nil)
        IPAddr.new(string)
      end
    end
    class InetEncoder < PG::SimpleEncoder
      def encode(ip_addr)
        ip_addr.to_s
      end
    end
    
    # 0 if for text format, can also be 1 for binary
    PG::BasicTypeRegistry.register_type(0, 'inet', InetEncoder, InetDecoder)
    
    需要“ipaddr”
    需要“pg”
    类InetDecoder
    对于那些试图在默认情况下转换字符串的人来说,这里有一个包罗万象的建议:

    client = PG.connect( DB[:pg] )           
    
    map = PG::BasicTypeMapForResults.new(conn)
    map.default_type_map = PG::TypeMapAllStrings.new
    
    client.type_map_for_results = map
    

    当然,我已经做了这些事情。我想,如果Pg能够通过将“Money”作为字符串传递给Codermap来支持Money,它可能已经这样做了?我非常确定我需要创建一个新的Coder对象,将数字和货币类型转换为BigDecimal。但到目前为止,我所看到的源代码中没有任何内容告诉我如何配置新的编码器对象,或者将其插入何处……以一个已定义的编码器/解码器PIR为例如何?“到目前为止,我所看到的源代码中没有任何东西告诉我如何配置一个新的编码器对象”PS经过这么多年的潜伏,我刚刚加入了so,这是我的第一个贡献。感谢您的指导;)您的代码中有一个输入错误(
    new\u coder
    ->
    name\u coder
    ),如果您显示错误消息,我会花两分钟而不是五分钟来检查您的答案。但这些都无关紧要。你已经解决了其他人无法解决的问题,即使我提供了赏金。滴答,滴答,滴答。非常感谢!由于我在实现中使用了
    name\u coder
    ,所以没有显示错误。我的字段实际上被恰当地命名为
    name
    ,因此我选择了
    name\u coder
    ,不幸的是,它在代码中呼应了
    c.name
    。。。困惑了吗?这就是为什么我(试图)改名的原因:P谢谢你发现了最后一个,我写这个答案的那一天几乎筋疲力尽了。很乐意帮忙!:)