Rake db: migrate utility fails due to duplicate column in my User - Rails 3.1 table

This is the error I get when I install Devise and run rake db:migrate :

 == AddDeviseToUsers: migrating =============================================== -- change_table(:users) rake aborted! An error has occurred, this and all later migrations canceled: SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL 

Given that this is just test data, I could just delete this column in my db and re-run it, but that doesn't seem very Railsy - if only for the reason that it will make my intermediate server (the only other server with my application ) out of sync with my localhost .

Also, if there is a conflict with another column.

So, given that this is a schema of my User table before starting the migration, how do I handle it? With some kind of migration that renames?

 # == Schema Information # # Table name: users # # id :integer not null, primary key # email :string(255) # f_name :string(255) # l_name :string(255) # username :string(255) # role_id :integer # picture :string(255) # about_me :string(255) # website :string(255) # created_at :datetime # updated_at :datetime # 
+6
source share
4 answers

Try the rake db: rollback command, and then try again. When you did this the first time, he added an id column. and why you

adding id: intege not null, primary key is automatic in rails. It should look like this.

 class CreateProducts < ActiveRecord::Migration def up create_table :products do |t| t.string :email t.text :f_name t.timestamps end end def down drop_table :products end end 

You can get more information here http://guides.rubyonrails.org/migrations.html

+4
source

In the migration file generated by Devise, change the line

  t.string :email, :null => false, :default => "" 

for

  t.change :email, :string, :null => false, :default => "" 

Therefore, instead of creating a new mailbox, the migration changes the existing one to the Devise specification.

+16
source

The resolution to this error is simple.

  • If you are already running rake db:migrate , I suggest running rake db:rollback "
  • Go to timestamp_add_devise_to_whatever.rb and comment out
  • # t.string :email, null: false, default: ""
  • Further also comment on this.
  • # add_index :users, :email, unique: true
  • Run rake db:migrate and your good. :)
+3
source

I find the same thing by adding development to an existing database. This fixed it for me:

Change the transfer of the automatically generated project:

 t.rename :email, :email_old # move my old email field out of the way ... #add_index :users, :email, :unique => true ## comment out unique index 

Migration db.

Make unique (new) records of email field data interactively with an SQL call:

 update users set email=id; 

Create a new migration by adding a unique constraint and run it:

 class UniquifyIndex < ActiveRecord::Migration def change add_index :users, :email, :unique => true end 
+2
source

Source: https://habr.com/ru/post/897519/


All Articles