heroku PGError:错误:关系“users”的“email”列已存在

pdtvr36n  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(133)

我一直在开发一个网站上localhost和它wors罚款。今天早上,我尝试使用命令“git push heroku master”和“heroku run rake db:migrate”将其推送到heroku。当我试着做第二个时,我有一个错误:

Connecting to database specified by DATABASE_URL
Migrating to DeviseCreateUsers (20130427200347)
Migrating to CreateAuthentications (20130427210108)
Migrating to AddTokenToAuth (20130427233400)
Migrating to AddNotificationToAuth (20130427234836)
Migrating to AddNotifToUser (20130428031013)
Migrating to AddDeviseToUsers (20130712103048)
==  AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "email" character varying(255) DEFAULT '' NOT N
sql_adapter.rb:652:in `async_exec'

字符串
我发现有人有同样的问题(heroku PGError: already exists 500 We're sorry, but something went wrong),但在我的情况下,迁移“AddDeviseToUsers”不在“db/migrate”文件夹中。
影响用户表的先前迁移包括:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
  create_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
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  ## Token authenticatable
  # t.string :authentication_token

  t.timestamps
end

add_index :users, :email,                :unique => true
add_index :users, :reset_password_token, :unique => true
end
end


和/或

class AddNotifToUser < ActiveRecord::Migration
 def change
  add_column :users, :notif, :string
 end
end


提前感谢!!
运行时编辑-回复评论:heroku运行rake db:migrate:status

up     20130427200347  Devise create users
up     20130427210108  Create authentications
up     20130427233400  Add token to auth
up     20130427234836  Add notification to auth
up     20130428031013  Add notif to user
down    20130712103048  Add devise to users
down    20130719091217  Create relationships
.
.
.

7gcisfzg

7gcisfzg1#

我在Devise migration on existing model找到了答案。
我所做的是在DeviseCreateUsers迁移中注解掉这一行:

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

字符串

wwwo4jvm

wwwo4jvm2#

此问题是在User型号带有email字段时引起的。解决方案是不要注解掉重要的一行,即使它是一个空字符串,它也会强制电子邮件具有值。
要解决这个问题,需要手动更改电子邮件列如下(我只保留相关部分):

class AddDeviseToUsers < ActiveRecord::Migration[7.0]
  def self.up
    change_table :users do |t|
      # t.string :email,              null: false, default: ''
      t.change_null :email, false
      t.change_default :email, ''

      t.string :encrypted_password, null: false, default: ''
    end
  end

  def self.down
    change_table :users do |t|
      t.change_null :email, true
      t.change_default :email, nil

      t.remove :encrypted_password, null: false, default: ''
    end
  end
end

字符串

相关问题