Rails数据库迁移失败,因为在添加设计器生成的用户表时“重复列名:email”

时间:2022-12-04 15:38:56

I am in the process of installing devise. I followed all the required steps and ended here:

我正在安装随身听。我按照所有要求的步骤,在这里结束:

$ rails generate devise User
$ rake db:migrate

When I run rake db:migrate I get the following error:

当我运行rake db: migration时,我得到以下错误:

$ 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)

My project has the following database schema. Some of the devise migration appears to be successful, but the migration was not completed.

我的项目有以下数据库模式。一些设计迁移看起来是成功的,但是迁移还没有完成。

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

What is the problem and how do I fix it?

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

2 个解决方案

#1


4  

So the problem is you already have the users table inside your database. You need to either drop that table and create a new one if it's not working or just continue using that table.

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

It's not a good idea to drop your database but since you are making a new app(as you creating a users table) so in your case it'll be better to drop that database and recreate it(to make sure everything works). Try these commands in sequence:

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

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

It should work fine and you need not do rails generate devise User as you already have your users migration file

它应该可以正常工作,并且不需要rails生成设计用户,因为您已经有了用户迁移文件

Update:

更新:

If you don't want to drop your database then you can create a new migration file and delete your users table there.

如果不想删除数据库,那么可以创建一个新的迁移文件并删除其中的users表。

rails generate migration DropUsersTable

after that edit your migration file

然后编辑您的迁移文件

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

and then do rake db:migrate

然后做rake db: migration

#2


0  

Solution: 1. Put all the migrations in migration folder in new archived_migrations folder. 2. db:schema:load 3. Run rake db:migrate

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

#1


4  

So the problem is you already have the users table inside your database. You need to either drop that table and create a new one if it's not working or just continue using that table.

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

It's not a good idea to drop your database but since you are making a new app(as you creating a users table) so in your case it'll be better to drop that database and recreate it(to make sure everything works). Try these commands in sequence:

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

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

It should work fine and you need not do rails generate devise User as you already have your users migration file

它应该可以正常工作,并且不需要rails生成设计用户,因为您已经有了用户迁移文件

Update:

更新:

If you don't want to drop your database then you can create a new migration file and delete your users table there.

如果不想删除数据库,那么可以创建一个新的迁移文件并删除其中的users表。

rails generate migration DropUsersTable

after that edit your migration file

然后编辑您的迁移文件

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

and then do rake db:migrate

然后做rake db: migration

#2


0  

Solution: 1. Put all the migrations in migration folder in new archived_migrations folder. 2. db:schema:load 3. Run rake db:migrate

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