【问题标题】:$ rails generate devise User #=> Generates migration using change_table instead of create_table? Devise Bug?$ rails generate devise User #=> 使用 change_table 而不是 create_table 生成迁移?设计错误?
【发布时间】:2018-02-01 16:37:22
【问题描述】:

我正在使用 Devise 设置我的 Rails 5 应用程序,使用的设置说明位于 Devise's GitHub page...

按照所有说明操作,直到我必须生成以前不存在的用户模型...

# $ rails generate devise MODEL 
$ rails generate devise User # => User did NOT previously exist!

生成的迁移文件使用 change_table 而不是 create_table...

# frozen_string_literal: true

class AddDeviseToUsers < ActiveRecord::Migration[5.1]
  def self.up
    # Why not create_table???
    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.inet     :current_sign_in_ip
      t.inet     :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

创建数据库后...

$ rails db:create # => Database created successfully

并尝试使用...进行迁移

$ rails db:migrate

我收到以下错误,这完全有道理 - users 表不存在:

== 20180201152148 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "users" does not exist

Devise 文档说明了以下内容,这意味着如果 MODEL 不存在,它将被创建:

这将创建一个模型(如果不存在)并使用 默认的设计模块。

现在文档可能指的是 MODEL.rb 文件,但这似乎根本没有意义。

我找不到任何与此相关的设计错误,但我看到了与以前存在 MODLE 时相关的错误。

问。我做错了什么还是这是一个设计错误?

**更新** 我只需将 change_table 更改为 create_table 即可解决此问题。我还将迁移文件从 add_devise_to_users 适当地重命名为 create_devise_users,但似乎我不应该这样做。

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-5


    【解决方案1】:

    没有收到关于这是否是 Devise 错误或我所做的事情的确认;然而,就是这样处理的。我希望它可以帮助其他困惑或遇到此特定问题的人:

    我是如何处理的...

    第一

    我在迁移文件中更改了以下行 from...

    change_table :users do |t|
    

    create_table :users do |t| # => changed 'change_' to 'create_'
    

    第二

    我重命名了我的迁移文件来自...

    /db/migrate/20180201152148_add_devise_to_users.rb 
    

    (更恰当)

    /db/migrate/20180201152148_create_devise_users.rb 
    

    第三

    我从命令行运行了我的迁移...

    $ rails db:migrate
    

    完成!

    【讨论】:

    • 这真的很糟糕。仍然没有来自 GitHub 问题的消息?不过感谢您的解决方法!
    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多