Ruby on rails Desive rake db:migrate失败,因为我的用户';s表-轨道3.1
这是我第一次安装Desive并运行Ruby on rails Desive rake db:migrate失败,因为我的用户';s表-轨道3.1,ruby-on-rails,ruby-on-rails-3,devise,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Devise,Ruby On Rails 3.1,这是我第一次安装Desive并运行rake db:migrate时遇到的错误: == AddDeviseToUsers: migrating =============================================== -- change_table(:users) rake aborted! An error has occurred, this and all later migrations canceled: SQLite3::SQLException: dupli
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
考虑到这只是测试数据,我可以删除数据库中的该列并重新运行它,但这似乎不太有意思——如果只是因为它会使我的登台服务器(我的应用程序的唯一其他服务器)与我的本地主机
不同步的话
此外,如果与另一列发生冲突,该怎么办
因此,考虑到这是运行迁移之前我的User
表的模式,我应该如何处理?通过某种类型的迁移进行重命名
# == 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
#
请尝试
rake db:rollback
,然后重试。当您第一次添加id列时。为什么要添加id:integer-not-null,主键
它在rails中是自动的。应该是这样的:
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
class CreateProducts
您可以在这里获得更多信息我在现有数据库中添加Desive时遇到了同样的问题。这为我解决了这个问题: 修改自动生成的设备迁移:
t.rename :email, :email_old # move my old email field out of the way
...
#add_index :users, :email, :unique => true ## comment out unique index
迁移数据库
通过SQL调用以交互方式使(新)电子邮件字段数据条目唯一:
update users set email=id;
创建另一个添加唯一约束的迁移,并运行它:
class UniquifyIndex < ActiveRecord::Migration
def change
add_index :users, :email, :unique => true
end
class UniquifyIndextrue
结束
在Desive生成的迁移文件中,更改行
t.string :email, :null => false, :default => ""
为了
因此,迁移将现有的电子邮件列更改为designe的规范,而不是尝试创建新的电子邮件列。解决此错误很简单
rakedb:migrate
”我建议运行rakedb:rollback
”timestamp\u add\u design\u to\u whatever.rb
并注释掉#t.string:email,null:false,默认值:“
#添加索引:用户,:电子邮件,唯一:真
不,在运行
db:migrate
之前,我的用户
模型中有该模式。我甚至在表中存储了一些用户的现有信息。然而,现在我正试图将Desive安装到现有的用户模型中,我得到了这个错误。所以,是的,我知道存在一个:email
列。我只想让它忽略它,或者按照需要修改的方式修改它。您可以跳过它,但在迁移过程中对我们的行进行注释。然后,再次运行rake db:migrate。是的,这里也有同样的问题,我所做的就是注释掉在designe迁移中创建电子邮件表的行,如#t.string:email,:null=>false,:default=>“”你太棒了-这很好用-stackoverflow总是有最差的答案作为选择-这是一个社会的错误!
t.change :email, :string, :null => false, :default => ""