Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Desive rake db:migrate失败,因为我的用户';s表-轨道3.1_Ruby On Rails_Ruby On Rails 3_Devise_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails Desive rake db:migrate失败,因为我的用户';s表-轨道3.1

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

这是我第一次安装Desive并运行
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,默认值:“
  • 下一步还要对此发表评论
  • #添加索引:用户,:电子邮件,唯一:真
  • 运行rake db:migrate和您的good to go.:)
    不,在运行
    db:migrate
    之前,我的
    用户
    模型中有该模式。我甚至在表中存储了一些用户的现有信息。然而,现在我正试图将Desive安装到现有的用户模型中,我得到了这个错误。所以,是的,我知道存在一个
    :email
    列。我只想让它忽略它,或者按照需要修改的方式修改它。您可以跳过它,但在迁移过程中对我们的行进行注释。然后,再次运行rake db:migrate。是的,这里也有同样的问题,我所做的就是注释掉在designe迁移中创建电子邮件表的行,如#t.string:email,:null=>false,:default=>“”你太棒了-这很好用-stackoverflow总是有最差的答案作为选择-这是一个社会的错误!
      t.change :email, :string,     :null => false, :default => ""