尝试$ rake db:migrate但得到此错误

时间:2014-02-05 15:29:42

标签: ruby-on-rails

这是我在终端中遇到的错误。有什么问题?

DeviseCreateUsers: migrating
-- create_table(:users) rake aborted! An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "users" already exists: CREATE TABLE 
  "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email"
  varchar(255) DEFAULT '' NOT NULL, "encrypted_password" varchar(255) 
  DEFAULT '' NOT NULL, "reset_password_token" varchar(255), 
  "reset_password_sent_at" datetime, "remember_created_at" datetime, 
  "sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at" 
  datetime, "last_sign_in_at" datetime, "current_sign_in_ip"
  varchar(255), "last_sign_in_ip" varchar(255), "created_at" datetime,
  "updated_at" datetime) 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `new'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `prepare' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in `execute' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `block in execute'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in > `instrument' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `execute'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in `create_table'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing' 
/Users/danieljuzovitskij/Desktop/pinteresting/db/migrate/20140205151417_devise_create_users.rb:3:in `change'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate' 
/Users/danieljuzovitskij/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate (See > full trace by running task with --trace)
------------------------------------------------------------------------

4 个答案:

答案 0 :(得分:0)

  

SQLite3 :: SQLException:表“users”已经存在:

该错误意味着您的“用户”表已经创建。删除表或确保稍后的迁移不会尝试重新创建它。

答案 1 :(得分:0)

您的迁移似乎与您的数据库不同步。如果这是一个开发环境,我建议你做以下的rake任务。

rake db:drop
rake db:create
rake db:migrate

答案 2 :(得分:0)

您失败的迁移称为DeviseCreateUsers。我建议检查this.以下是将Devise添加到已创建的用户模型的方法。

我建议您回滚上次更改(例如创建失败的迁移)并执行此操作。只是为了保持一致:

rails g devise user

将添加必要的迁移并检查表是否已创建。

答案 3 :(得分:0)

使用 force :: true

create_table :users, force: true do |t|
 // your contents
end

它将删除现有表并创建新表。