I'm setting up my Rails 5 application with Devise using the setup instructions found on Devise's GitHub page...
After following all the instructions up to where I have to generate my User model that did NOT previously exist...
# $ rails generate devise MODEL
$ rails generate devise User # => User did NOT previously exist!
The migration file produced uses change_table as opposed to 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
After I create my database...
$ rails db:create # => Database created successfully
And try to migrate using...
$ rails db:migrate
I get the following error, which makes complete sense - the users table does not exist:
== 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
The Devise documentation states the following which implies that if MODEL is does not exist, it will be create:
This will create a model (if one does not exist) and configure it with the default Devise modules.
Now perhaps the documentation is referring to the MODEL.rb file, but this doesn't seem like it makes sense at all.
I can't find any Devise bugs related to this, but I see bugs related to when MODLE DOES previously exist.
Q. Did I do something wrong or is this a Devise bug?
**UPDATE** I was able to fix this by simply changing change_table to create_table. I also appropriately renamed the migration file from to add_devise_to_users to create_devise_users, but it seems I shouldn't have to do this.
I've not received a confirmation yet as to whether or not this is a Devise bug or something I did; however, this is how I dealt with it. I hope it helps anyone else who is confused, or, is having this particular issue:
How I dealt with this...
First
I changed the following line in my migration file from...
change_table :users do |t|
to
create_table :users do |t| # => changed 'change_' to 'create_'
Second
I renamed my migration file from...
/db/migrate/20180201152148_add_devise_to_users.rb
to (more appropriately)
/db/migrate/20180201152148_create_devise_users.rb
Third
I ran my migration from the command line...
$ rails db:migrate
Done!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With