【发布时间】: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