发生错误,此以及所有后续迁移都已取消[Rails DB错误]

时间:2013-05-28 20:14:32

标签: ruby-on-rails ruby ruby-on-rails-3

提供一些背景知识:我一直在构建一个个人博客,与任何其他博客一样,它要求用户身份验证(或至少一个用户)在数据库中。因此,在开发过程中,我使用rails控制台创建了一个用户。

然而,在制作中我需要一个身份验证系统才能创建帖子。所以我所做的是尝试安装设备来处理这个问题,尽管我收到的错误如下所示。

我知道问题与我的数据库中已有的用户表有关,但问题是 - 当尝试运行rake db:migrate以删除表时我得到一个错误,最终,我有在停滞不前。

请告知。

github:https://github.com/Apane/Blogit_blog

rake db:migrate
==  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(128) DEFAULT '' NOT NULL, "password_salt" varchar(255) DEFAULT '' NOT NULL, "reset_password_token" varchar(255), "remember_token" varchar(255), "remember_created_at" datetime, "sign_in_count" integer DEFAULT 0, "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 NOT NULL, "updated_at" datetime NOT NULL) /usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:466:in `block in method_missing'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `block in say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:458:in `method_missing'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:334:in `method_missing'
/Users/user/Sites/personal_blog/personal_blog/db/migrate/20130528200110_devise_create_users.rb:3:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:370:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:410:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:389:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:528:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:719:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:570:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/migration.rb:551:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

2 个答案:

答案 0 :(得分:1)

你的问题在于它在错误中声明:

rake db:migrate
==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users

所以发生了什么我相信你试图迁移当前已存在的users表。我也注意到你正在使用设计。您是否运行了以下命令:

rails g devise:install

rails g devise user

rake db:migrate

您还应该将schema.rb版本与db / migrate / * .rb文件进行比较。

<强>更新

您确定要连接到同一个数据库吗?因为我刚注意到你说过

  

在开发中,我使用rails控制台创建了一个用户。

但在production你遇到了问题。在我看来,您可能无法连接到同一个数据库。另外,建议不要使用SQLite3进行生产。

请检查您要连接到哪个数据库的环境。如果您在Heroku上发布应用程序,这是一种很好的做法。您可能会注意到这使用了Postgres。良好的做法是使用相同的数据库进行开发和生产。

一旦你发现你要连接的数据库和它是什么,我相信这可能是问题所在。

此外,如果您尝试在生产中迁移或类似的东西,您可以使用以下内容:bundle exec rake db:migrate RAILS_ENV=production这将执行生产环境的迁移

答案 1 :(得分:1)

为您的问题

  

SQLite3 :: SQLException:表“users”已存在:CREATE TABLE“users”(“id”INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,“email”varchar(255)DEFAULT''NOT NULL,“encrypted_pa​​ssword”varchar(128 )DEFAULT''NOT NULL,“password_salt”varchar(255)DEFAULT''NOT NULL,“reset_password_token”varchar(255),“remember_token”varchar(255),“remember_created_at”datetime,“sign_in_count”integer DEFAULT 0,“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 NOT NULL,”updated_at“datetime NOT NULL)

也许您可以检查数据库中的schema_migrations版本。如果您发现您的用户表版本号(我的意思是在您的db / migrate / xxxxxxxxxxxxxx_devise_create_users.rb文件中,并且xxxxxxxxxxxxxx是您的版本号)是在表中的版本后面并且您的users表已经存在,则可以添加用户表版本自己编号跳过创建操作。