所以我在rails中有一些迁移问题..我有2个迁移,一个用于添加users表,另一个用于向用户添加设计...
现在我尝试运行
时出现此错误 rake db:migrate
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar DEFAULT '' NOT NULL
告诉我两个迁移都试图将列电子邮件添加到users表中。
USER TABLE CREATE MIGRATION
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password_digest
t.timestamps null: false
end
end
end
添加到用户迁移的设备
class AddDeviseToUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your original model.
# t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
我假设它的
add_index :users, :email, unique: true
在第二次迁移中导致这个问题...但我只是好奇......这条线甚至与设计相关吗?我在文档中找不到与此相关的任何内容...所以如果我要删除那两行会对 设计 的运行方式产生影响吗?
答案 0 :(得分:0)
您正尝试两次创建列email
:在您自己的迁移和设计中。此外,您不需要password_digest
列。第二次因为列已经存在而出现错误。
我的建议是在创建用户之前回滚版本(rake db:rollback VERSION = timestamp_from_migration_filename),从CreateUsers中删除email和password_digest,然后重试所有迁移。
答案 1 :(得分:0)
change_column helper用于对单个表进行多次更改。它总是尝试添加列。
请查看api-dock
上的详细信息