[英]$ rails generate devise User #=> Generates migration using change_table instead of create_table? Devise Bug?
我將使用Devise的GitHub頁面上的設置說明,通過Devise設置我的Rails 5應用程序...
按照所有說明進行操作后,直到必須生成以前不存在的用戶模型為止。
# $ 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,則會創建該模型:
這將創建一個模型(如果不存在),並使用默認的Devise模塊進行配置。
現在,也許文檔正在引用MODEL.rb文件,但這似乎根本沒有道理。
我找不到與此相關的任何Devise錯誤,但我看到與以前存在MODLE時相關的錯誤。
問:我做錯什么了嗎?或者這是一個設計錯誤?
**更新**我能夠通過簡單地將change _table 更改為create _table來解決此問題。 我也將遷移文件從適當地重命名為add_devise_to_users到create_devise_users ,但似乎我不必這樣做。
我還沒有收到確認還沒有為這是否是一個設計錯誤或我做的事; 但是,這就是我的處理方式。 我希望它可以幫助其他困惑或正在遇到此特定問題的人:
我該如何處理...
第一
我將遷移文件中的以下行從...更改為
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
完成!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.