1

当我在尝试迁移我的数据库时遇到错误时,我正在学习 Ruby on Rails 3 教程。即使我在运行 db:migrate 之前执行了 db:drop,也会出现“无法将 SQLite3::Statement 转换为数组”错误。我尝试更新 rake 并从我的 Gemfile 中删除 rspec,但都没有为我工作。

rake db:migrate -t
** Invoke db:migrate (first_time)
** Invoke environment (first_time)   
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env 
** Execute db:load_config
** Execute db:migrate
   rake aborted!
   can't convert SQLite3::Statement into Array
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/result.rb:30:in `zip'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/result.rb:30:in    `hash_rows'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/result.rb:29:in `map'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/result.rb:29:in `hash_rows'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/result.rb:20:in `each'
   /Library/Ruby/Gems/1.8/gems/activerecord- 3.1.1/lib/active_record/connection_adapters/sqlite_adapter.rb:315:in `map'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/connection_adapters/sqlite_adapter.rb:315:in `tables'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:25:in  `table_exists?'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:417:in `initialize_schema_migrations_table'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/migration.rb:634:in `initialize'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/migration.rb:549:in `new'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/migration.rb:549:in `up'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/migration.rb:530:in `migrate'
   /Library/Ruby/Gems/1.8/gems/activerecord-3.1.1/lib/active_record/railties/databases.rake:161
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
     /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
   /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
   /usr/bin/rake:19:in `load'
   /usr/bin/rake:19
   Tasks: TOP => db:migrate

我正在使用的宝石是:

Using rake (0.9.2.2) 
Using multi_json (1.0.4) 
Using activesupport (3.1.1) 
Using builder (3.0.0) 
Using i18n (0.6.0) 
Using activemodel (3.1.1) 
Using erubis (2.7.0) 
Using rack (1.3.5) 
Using rack-cache (1.1) 
Using rack-mount (0.8.3) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.0.3) 
Using actionpack (3.1.1) 
Using mime-types (1.17.2) 
Using polyglot (0.3.3) 
Using treetop (1.4.10) 
Using mail (2.3.0) 
Using actionmailer (3.1.1) 
Using arel (2.2.1) 
Using tzinfo (0.3.31) 
Using activerecord (3.1.1) 
Using activeresource (3.1.1) 
Using bundler (1.0.21) 
Using coffee-script-source (1.1.3) 
Using execjs (1.2.12) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using json (1.6.3) 
Using rdoc (3.12) 
Using thor (0.14.6) 
Using railties (3.1.1) 
Using coffee-rails (3.1.1) 
Using jquery-rails (1.0.19) 
Using nokogiri (1.5.0) 
Using rails (3.1.1) 
Using sass (3.1.12) 
Using sass-rails (3.1.5) 
Using sqlite3 (1.3.5) 
Using sqlite3-ruby (1.2.5) 
Using uglifier (1.2.0) 
Using webrat (0.7.1)

真的很困惑这个错误

迁移文件:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end


class CreateItems < ActiveRecord::Migration
  def change
    create_table :items do |t|
      t.string :title
      t.string :description
      t.double :price
      t.string :category

      t.timestamps
    end
  end
end

使用 ruby​​ 1.9.2 而不是 1.8.7 会导致:

    rake aborted!
    NoMethodError: undefined method `to_a' for #<SQLite3::Statement:0x007f9404b73b18>:
          SELECT name
          FROM sqlite_master
          WHERE type = 'table' AND NOT name = 'sqlite_sequence'

    Tasks: TOP => db:migrate
4

1 回答 1

3

通过修改 Gemfile 以使用以下方法解决了该问题:

gem 'sqlite3', '1.3.5', :require => 'sqlite3'

代替

gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'

教程中提供的

于 2011-12-22T21:14:20.067 回答