This is the error I get when I am first installing Devise and running 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 that column in my db and re-run it, but that doesn't seem very Railsy - if only for the reason that it will making my staging server (the only other server with my app) out-of-sync with my localhost
.
Also, what if there is a conflict with another column.
So given that this is the schema of my User
table before running开发者_运维问答 the migration, how should I handle this? With a migration of some sort that does a rename?
# == 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
#
In the migration file generated by Devise, change the line
t.string :email, :null => false, :default => ""
for
t.change :email, :string, :null => false, :default => ""
So instead of trying to create a new email colum, the migration changes the existing one to the specifications of Devise.
Try rake db:rollback
and then try again. When you did it the first time it added the id column. and why are you adding id :integer not null, primary key
it's 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
Resolving this error is simple
- if you already run the "
rake db:migrate
" i suggest runrake db:rollback
" - Go to "
timestamp_add_devise_to_whatever.rb
and comment out # t.string :email, null: false, default: ""
- Next also comment this
# add_index :users, :email, unique: true
- Run
rake db:migrate
and your good to go. :)
I hit the same thing adding devise to an existing DB. This fixed it for me:
Modify the autogenerated devise migration:
t.rename :email, :email_old # move my old email field out of the way
...
#add_index :users, :email, :unique => true ## comment out unique index
Migrate the db.
Make the (new) email field data entries unique interactively w/ a SQL call:
update users set email=id;
Create another migration adding the unique constraint, and run it:
class UniquifyIndex < ActiveRecord::Migration
def change
add_index :users, :email, :unique => true
end
精彩评论