在PostgreSQL中使用Sequel gem插入数组 [英] Inserting an array using Sequel gem in PostgreSQL

查看:115
本文介绍了在PostgreSQL中使用Sequel gem插入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用以下模式创建了一个表

i created a table with the following schema in code

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

满足需求,并提供我要使用以下代码插入字符串数组

for needs and offering i am inserting a string array with following code

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

任何人都可以告诉合适的方式在Sequel gem中插入数组以及如何在列中添加文本数组

can any one please tell a suitable way how to insert array in Sequel gem and how to add Text array in a column

推荐答案

您需要使用Sequel的pg_array扩展名,请参见< a href = http://sequel.rubyforge.org/rdoc-plugins/files/l ib / sequel / extensions / pg_array_rb.html rel = nofollow> http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/pg_array_rb.html

You need to use Sequel's pg_array extension, see http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/pg_array_rb.html

这篇关于在PostgreSQL中使用Sequel gem插入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆