0

使用以下语句:

rails generate scaffold Product title:string description:text image_url:string price:decimal

并稍微编辑生成的迁移我有以下迁移:

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :title
      t.text :description
      t.string :image_url
      t.decimal :price, precision: 8, scale: 2

      t.timestamps
    end
  end
end

之后,我成功运行了以下命令:

rake db:migrate

并连接到我的数据库并检查是否确实创建了表。然后,我跑

rake test

并得到了很多错误,我相信问题的根源是rails无法找到“产品”表:

 1) Error:
test_should_create_product(ProductsControllerTest):
ActiveRecord::JDBCError: Table products does not exist
    arjdbc/jdbc/RubyJdbcConnection.java:115:in `columns'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-jdbc-adapter-1.2.7/lib/arjdbc/db2/adapter.rb:514:in `columns'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:660:in `column_names'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:651:in `timestamp_column_names'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:585:in `table_rows'
    org/jruby/RubyHash.java:1257:in `each'
    org/jruby/RubyEnumerable.java:718:in `map'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:579:in `table_rows'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:494:in `create_fixtures'
    org/jruby/RubyArray.java:1613:in `each'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:492:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:491:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:168:in `disable_referential_integrity'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:476:in `create_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:895:in `load_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activerecord-3.2.12/lib/active_record/fixtures.rb:849:in `setup_fixtures'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:432:in `_run__858744690__setup__1004892786__callbacks'
    org/jruby/RubyBasicObject.java:1659:in `__send__'
    org/jruby/RubyKernel.java:2086:in `send'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:390:in `_run_setup_callbacks'
    org/jruby/RubyBasicObject.java:1659:in `__send__'
    org/jruby/RubyKernel.java:2086:in `send'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /home/gotqn/.rvm/gems/jruby-1.7.2/gems/activesupport-3.2.12/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

据我了解,在进行测试时,我的“测试”数据库被删除,“开发”数据库的迁移适用于它。因为,我使用的是“IBM DB2 Express 10.1”数据库,我想我需要对负责测试的文件进行一些更改。

你能帮我解决这个问题吗?

下面,我展示了有关整个情况的更多信息:

  1. 使用“IBM DB2 Express”版本 10.1
  2. 使用 jRuby 1.7.2
  3. 使用 Rails 版本 3.2.12
  4. 使用 Ubuntu 10.04 LTC
  5. 这就是我的“database.yml”文件的样子:
development:
    adapter: jdbc
    driver: com.ibm.db2.jcc.DB2Driver
    url: jdbc:db2://localhost:50000/ddevelop
    host: localhost
    port: 50000
    database: ddevelop
    username: db2inst1
    password: pass

test:
    adapter: jdbc
    driver: com.ibm.db2.jcc.DB2Driver
    url: jdbc:db2://localhost:50000/dtest
    host: localhost
    port: 50000
    database: dtest
    username: db2inst1
    password: pass

注意:“dtest”数据库已经创建。

4

1 回答 1

1

测试数据库尚未更新。您需要rake db:test:preparerake db:migration. 您现在可以运行它并重试。

于 2013-02-23T16:41:35.650 回答