ruby-on-rails – 在PostgreSQL中使用Sequel gem插入一个数组

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在PostgreSQL中使用Sequel gem插入一个数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在代码中创建了一个包含以下模式的表
  1. DB.create_table :Pokemon do
  2. primary_key :id
  3. String :first_name
  4. String :last_name
  5. String :email
  6. String :zipcode
  7. String :company_name
  8. String :google_profile
  9. String :skype
  10. String :phone
  11. String :about
  12. String :linkedin_profile_url
  13. String :company_url
  14. column :needs,'Text[]'
  15. column :offering,'Text[]'
  16. end

为了需要和提供我插入一个字符串数组与以下代码

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

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

  1. PG::Error: ERROR: column "offering" is of type text[] but expression is of type record (Sequel::DatabaseError)
  2. LINE 1: ...fering","needs") VALUES ('abcd','mehandiratta',('hello1',...
  3. ^
  4. HINT: You will need to rewrite or cast the expression.
  5. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query'
  6. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/logging.rb:33:in `log_yield'
  7. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:145:in `execute_query'
  8. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute'
  9. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:111:in `check_disconnect_errors'
  10. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:132:in `execute'
  11. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:413:in `_execute'
  12. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
  13. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:425:in `check_database_errors'
  14. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
  15. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize'
  16. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
  17. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/database/connecting.rb:236:in `synchronize'
  18. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:242:in `execute'
  19. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:801:in `execute'
  20. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/postgres.rb:525:in `fetch_rows'
  21. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:860:in `returning_fetch_rows'
  22. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/dataset/actions.rb:341:in `insert'
  23. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1060:in `insert'
  24. from /var/lib/gems/1.8/gems/sequel-3.43.0/lib/sequel/adapters/shared/postgres.rb:1069:in `insert'
  25. from hello.rb:10

任何人都可以告诉一个合适的方法如何在Sequel gem中插入数组以及如何在列中添加Text数组

解决方法

猜你在找的Ruby相关文章