设计注册 Ruby on Rails - 迁移错误:重复的列名称

问题描述 投票:0回答:1

我在运行将 Devise 添加到我的用户表的 Rails 迁移时遇到问题。错误消息指向重复的列名称,特别是“电子邮件”。导致问题的迁移文件位于 /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb。

这是错误:

`➜ pye-candles git:(master) ✗rails db:migrate == 20231115201715 AddDeviseToUsers:正在迁移================================== --change_table(:用户) 铁轨已中止! StandardError:发生错误,此迁移和所有后续迁移均已取消:

SQLite3::SQLException:重复的列名:电子邮件 /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:7:in

block in up' /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:5:in 
up'

原因: ActiveRecord::StatementInvalid: SQLite3::SQLException: 重复的列名: 电子邮件 /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:7:in

block in up' /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:5:in 
up'

原因: SQLite3::SQLException:重复的列名:电子邮件 /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:7:in

block in up' /Users/jaydenthelwell/pye-candles/pye-candles/db/migrate/20231115201715_add_devise_to_users.rb:5:in 
up' 任务:TOP => db:migrate (通过使用 --trace 运行任务查看完整跟踪) ➜ pye-candles git:(master) ✗ `

这是相关的迁移文件:

`# freeze_string_literal: true

类 AddDeviseToUsers < ActiveRecord::Migration[7.0] 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

结束

def self.down # 默认情况下,我们不想对如何回滚迁移做出任何假设 # 模型已经存在。请在下面编辑您想要在此迁移中删除的字段。 引发 ActiveRecord::IrreversibleMigration 结尾 结束

`

我删除了用户迁移文件,因为我认为这是导致问题的原因,用户表也有“电子邮件”,但问题仍然存在。

ruby ruby-on-rails-3 devise user-registration
1个回答
0
投票

遇到 SQLite3::SQLException:重复的列名:电子邮件错误后,我重新访问了我的 create_users 迁移文件。出现此问题的原因是 Devise 尝试添加的列已存在于原始迁移中。

为了解决此问题,我在迁移中重新创建了用户表,如下所示:

class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      # t.string :email, null: false, default: ""
      # t.string :encrypted_password, null: false, default: ""
      # t.string :reset_password_token
      # t.datetime :reset_password_sent_at
      # t.datetime :remember_created_at
      t.string :first_name
      t.string :second_name
      t.date :d_o_b
      t.string :phone_number
      t.string :address

      ## Other Devise fields if needed

      t.timestamps null: false
    end

    add_index :users, :email, unique: true
    add_index :users, :reset_password_token, unique: true
    # Add other indexes if needed
  end
end

但是注释掉了

  # t.string :email, null: false, default: ""
  # t.string :encrypted_password, null: false, default: ""
  # t.string :reset_password_token
  # t.datetime :reset_password_sent_at
  # t.datetime :remember_created_at

我注释掉了 Devise 尝试添加的列(电子邮件、加密密码等),因为这些列已包含在原始迁移中。进行此调整后,迁移成功运行,没有任何问题。

我希望这可以帮助其他面临类似问题的人!

© www.soinside.com 2019 - 2024. All rights reserved.