“在AddIndexToUsersEmail

时间:2015-06-12 07:56:40

标签: ruby-on-rails ruby ruby-on-rails-4

我正在关注建模用户的RailsTutorial电子书(第6章)。我正在尝试通过运行此命令向用户电子邮件添加索引:

rails generate migration add_index_to_users_email

哪个给我db/migrate/[timestamp]_add_index_to_users_email.rb文件:

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true # I add this line
  end
end

但是当我尝试运行bundle exec rake db:migrate命令时,我会在终端内找到它:

== 20150612073526 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
   -> 0.0015s
== 20150612073526 AddIndexToUsersEmail: migrated (0.0016s) ====================

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::BusyException: database is locked: commit transaction/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `step'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `block in each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `loop'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `map'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `block in execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:95:in `prepare'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:517:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `block in commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:155:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:179:in `commit_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:198:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: commit transaction
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `step'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `block in each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `loop'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `map'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `block in execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:95:in `prepare'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:517:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `block in commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:155:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:179:in `commit_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:198:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
SQLite3::BusyException: database is locked
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `step'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:108:in `block in each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `loop'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/statement.rb:107:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `map'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:149:in `block in execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:95:in `prepare'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:517:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `block in commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:361:in `commit_db_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:155:in `commit'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:179:in `commit_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:198:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

这是我的Gemfile btw:

source 'https://rubygems.org'

gem 'rails',        '4.2.0'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails',   '5.0.2'
gem 'uglifier',     '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks',   '2.3.0'
gem 'jbuilder',     '2.2.3'
gem 'sdoc',         '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3',     '1.3.9'
  gem 'byebug',      '3.4.0'
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

group :test do
  gem 'minitest-reporters', '1.0.5'
  gem 'mini_backtrace', '0.1.3'
  gem 'guard-minitest', '2.3.1'
end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

这与会话问题有关,检查一些其他会话是打开rails s还是rails c。关闭它们将解决问题。

答案 1 :(得分:1)

您的 rails控制台打开时,可能正在执行迁移。因此,您需要在打开的rails控制台程序中执行以下命令:

ActiveRecord::Base.connection.execute("BEGIN TRANSACTION; END;")