插入使用的PostgreSQL续集宝石的阵列(Inserting an array using Se

2019-07-19 02:02发布

我创建了一个表,在代码下面的模式

DB.create_table :Pokemon do
  primary_key :id
  String :first_name
  String :last_name
  String :email
  String :zipcode
  String :company_name
  String :google_profile
  String :skype
  String :phone
  String :about
  String :linkedin_profile_url
  String :company_url
  column :needs , 'Text[]'
  column :offering , 'Text[]'
end

对于需求并提供我正在将一个字符串数组与下面的代码

pokes=DB[:Pokemon];
off=['hello1' , 'hello2']
nee= ['need1' , 'need2']
pokes.insert(:first_name => 'abcd' ,:last_name => 'mehandiratta', :offering => off , :needs =>  nee)

当我运行它我得到一个错误

PG::Error: ERROR:  column "offering" is of type text[] but expression is of type record (Sequel::DatabaseError)
LINE 1: ...fering", "needs") VALUES ('abcd', 'mehandiratta', ('hello1',...
                                                             ^
HINT:  You will need to rewrite or cast the expression.
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/logging.rb:33:in `log_yield'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:111:in `check_disconnect_errors'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:413:in `_execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:425:in `check_database_errors'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:801:in `execute'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:525:in `fetch_rows'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:860:in `returning_fetch_rows'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:341:in `insert'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1060:in `insert'
    from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1069:in `insert'
    from hello.rb:10

任何一个可以告诉一个合适的方式如何在续集宝石插入阵列,以及如何在一列中添加文本阵列

Answer 1:

您需要使用续集的pg_array扩展,见http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/pg_array_rb.html



Answer 2:

正如错误说,只投数组表达式为字符串,像这样:

pokes.insert(:first_name => 'abcd' ,:last_name => 'mehandiratta', :offering => off.to_s , :needs =>  nee.to_s)


Answer 3:

在你的数据库的配置:

DB.extension :pg_array

然后,只需将每个阵列输入PGArray:

pokes.insert(:first_name => 'abcd' ,:last_name => 'mehandiratta', :offering => Sequel.pg_array(off) , :needs => Sequel.pg_array(nee))

@Jeremy埃文斯的回答提供了一个链接,其中包括一个更简洁的方式,如果使用这样做的文档core-extensions延伸。



文章来源: Inserting an array using Sequel gem in PostgreSQL