ruby Rails数据库迁移失败,出现“重复列名:添加devise生成的User表时,会显示“email

lokaqttq  于 2023-04-05  发布在  Ruby
关注(0)|答案(3)|浏览(76)

我正在安装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

问题是什么?我如何解决它?

mzmfm0qo

mzmfm0qo1#

所以问题是你的数据库中已经有了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

ibrsph3r

ibrsph3r2#

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

cotxawn7

cotxawn73#

Rails数据库迁移失败,出现“重复列名

如果你得到上面错误,是因为你在db/migrate文件夹中有一个重复的.rb文件,只要删除它,工作就完成了!!

相关问题