Rails数据库迁移失败,重复列名称为:email"添加设备生成的用户表时

时间:2014-06-18 02:25:48

标签: ruby-on-rails ruby devise database-migration

我正在安装devise。我按照所有必要的步骤结束了这里:

$ rails generate devise User
$ rake db:migrate

当我运行rake db:migrate时,我收到以下错误:

$ rake db:migrate
== 20140618020442 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "em
ail" varchar(255) DEFAULT '' NOT NULLc:/appname/db/migrate/20140618020442_add_d
evise_to_users.rb:5:in `block in up'
c:/appname/db/migrate/20140618020442_add_devise_to_users.rb:3:in `up'
c:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我的项目具有以下数据库架构。某些devise迁移似乎是成功的,但迁移尚未完成。

ActiveRecord::Schema.define(version: 20140618020442) do

  create_table "listings", force: true do |t|
    t.string   "name"
    t.text     "description"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

问题是什么,我该如何解决?

2 个答案:

答案 0 :(得分:4)

所以问题是你已经有了数据库中的users表。您需要删除该表并创建一个新表,如果它不起作用或只是继续使用该表。

删除数据库不是一个好主意,但是因为你正在创建一个新的应用程序(当你创建一个用户表时),所以在你的情况下,删除该数据库并重新创建它会更好(以确保一切正常)。按顺序尝试这些命令:

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

它应该可以正常工作,您无需执行rails generate devise User,因为您已拥有用户迁移文件

<强>更新

如果您不想删除数据库,则可以创建新的迁移文件并删除其中的用户表。

rails generate migration DropUsersTable

之后编辑您的迁移文件

class DropUsersTable < ActiveRecord::Migration
  def change
    drop_table :users
  end
end

然后执行rake db:migrate

答案 1 :(得分:0)

解决方案: 1.将迁移文件夹中的所有迁移放入新的archived_migrations文件夹中。 2. db:schema:load 3.运行rake db:migrate