为什么我被要求运行' rake db:migrate RAILS_ENV = test'?

时间:2015-06-27 12:26:14

标签: ruby-on-rails ruby

我一直在关注教程"多租户与Rails"作者Ryan Bigg https://leanpub.com/multi-tenancy-rails。 虽然我运行了rake db:migrate但我一直收到错误迁移正在等待中。要解决此问题,请运行bin / rake db:migrate RAILS_ENV = test 如果我这样做,我会得到另一组错误。 由于我没有足够的声誉,我无法在此处发布图片。 如果有人看过这本书,我会在添加add_owner_id_to_accounts迁移后收到错误。

任何帮助都会非常感激。谢谢!

这是我得到的错误:

    /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:392:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

    bin/rake db:migrate RAILS_ENV=test

    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:405:in `load_schema_if_pending!'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:411:in `block in maintain_test_schema!'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:639:in `suppress_messages'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:416:in `method_missing'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:411:in `maintain_test_schema!'
    from /home/nikhil/workspace2/subscribem/spec/rails_helper.rb:29:in `<top (required)>'
    from /home/nikhil/workspace2/subscribem/spec/features/accounts/sign_up_spec.rb:1:in `require'
    from /home/nikhil/workspace2/subscribem/spec/features/accounts/sign_up_spec.rb:1:in `<top (required)>'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1327:in `load'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1325:in `each'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:102:in `setup'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:88:in `run'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:73:in `run'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:41:in `invoke'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/exe/rspec:4:in `<top (required)>'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
    from /home/nikhil/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'

如果我运行rake db,这就是我得到的:migrate RAILS_ENV = test(因为输出要求我)

/home/nikhil/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/tasks/statistics.rake:4: warning: already initialized constant STATS_DIRECTORIES
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/tasks/statistics.rake:4: warning: previous definition of STATS_DIRECTORIES was here
== 20150627112900 CreateSubscribemAccounts: migrating =========================
-- create_table(:subscribem_accounts)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "subscribem_accounts" already exists: CREATE TABLE "subscribem_accounts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:208:in `create_table'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:661:in `block in method_missing'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `block in say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:651:in `method_missing'
/home/nikhil/workspace2/subscribem/db/migrate/20150627112900_create_subscribem_accounts.rb:3:in `change'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:605:in `exec_migration'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:589:in `block (2 levels) in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:588:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:587:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:764:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `block in ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:955:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `each'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `up'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "subscribem_accounts" already exists: CREATE TABLE "subscribem_accounts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:208:in `create_table'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:661:in `block in method_missing'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `block in say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:651:in `method_missing'
/home/nikhil/workspace2/subscribem/db/migrate/20150627112900_create_subscribem_accounts.rb:3:in `change'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:605:in `exec_migration'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:589:in `block (2 levels) in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:588:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:587:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:764:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `block in ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:955:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `each'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `up'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
SQLite3::SQLException: table "subscribem_accounts" already exists
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:208:in `create_table'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:661:in `block in method_missing'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `block in say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `say_with_time'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:651:in `method_missing'
/home/nikhil/workspace2/subscribem/db/migrate/20150627112900_create_subscribem_accounts.rb:3:in `change'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:605:in `exec_migration'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:589:in `block (2 levels) in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:588:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:587:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:764:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `block in ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `ddl_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:955:in `block in migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `each'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `up'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/nikhil/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate => app:db:migrate
(See full trace by running task with --trace)

2 个答案:

答案 0 :(得分:0)

在test_helper.rb中 -

ActiveRecord::Migration.maintain_test_schema!

每当您运行测试时,它都会每次都维护您的测试数据库。

答案 1 :(得分:0)

似乎问题的根源在于:

SQLite3::SQLException: table "subscribem_accounts" already exists

subscribem_accounts表似乎已经存在,而且就像你试图创建它两次一样,所以尝试重置数据库:

rake db:reset

此命令将删除您的实际数据库,并再次为您重新创建