1

I just recently deployed my app to Heroku however when I run Heroku run rake db:migrate I get the below errors.

 NameError: uninitialized constant User
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:292:in `get'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:81:in `to'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:76:in `modules'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:93:in `routes'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:160:in `default_used_route'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:70:in `initialize'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:326:in `new'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:326:in `add_mapping'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:238:in `block in devise_for'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:237:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:237:in `devise_for'
/app/config/routes.rb:4:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `eval_block'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:412:in `draw'
/app/config/routes.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:16:in `reload!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `execute'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:27:in `updater'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:328:in `require_environment!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:457:in `block in run_tasks_blocks'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

Below is my devise create user table (20160403200237_devise_create_users.rb)

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_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


      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
end

Below is an added column called (20160403200704_add_email_to_users.rb)

class AddEmailToUsers < ActiveRecord::Migration
  def change
    add_column :users, :playlist, :text
  end
end

Below is the user model (user.rb)

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  serialize  :playlist, Hash
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

Only when I run rake db:migrate in heroku do I get these errors. When I run locally, and even with rails s -e production for production mode things run smoothly. I could really use help on this one. Thanks!

Below is my Gemfile

source 'https://rubygems.org'
gem 'figaro'
gem 'bootstrap-sass'
gem 'font-awesome-sass'
gem 'devise'
gem 'rails_12factor', group: :production

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end 
acampbe222
  • 73
  • 1
  • 12
  • try `heroku run rake db:migrate:reset` – 7urkm3n Apr 03 '16 at 20:59
  • also, did u add `gem 'pg' and gem 'rails_12factor'` ? – 7urkm3n Apr 03 '16 at 21:00
  • have you tried doing a schema load? – Noam Hacker Apr 03 '16 at 21:00
  • @7urkm3n When i ran `heroku run rake db:migrate:reset` I get the following is my error: `Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"devglk6ordonlf", "username"=>"lfpwedxtmbnezx", "password"=>"Q5TaFPZIkTbiUQl2UZXTLrghH2", "port"=>5432, "host"=>"ec2-54-235-93-178.compute-1.amazonaws.com"} rake aborted! NameError: uninitialized constant User` – acampbe222 Apr 03 '16 at 21:04
  • try this one `heroku pg:reset DATABASE` where database name of DB ! – 7urkm3n Apr 03 '16 at 21:09
  • @Noam I get NameError: uninitialized constant User when I run 'heroku run rake db:schema:load' – acampbe222 Apr 03 '16 at 21:09
  • can you share your gemfile ? – 7urkm3n Apr 03 '16 at 21:13
  • @7urkm3n I just added my Gemfile – acampbe222 Apr 03 '16 at 21:33
  • can you move your `gem pg and raiils_12factor` => `group :production do gem 'pg' gem 'rails_12factor' end` – 7urkm3n Apr 03 '16 at 21:33
  • You just need to reset `heroku run pg:reset DATABASE`, then `heroku run rake db:migrate`. all should be fine. – 7urkm3n Apr 03 '16 at 21:37
  • @7urkm3n should I run bundle install after putting ` group :production do gem 'pg' gem 'rails_12factor' end ` in my Gemfile? – acampbe222 Apr 03 '16 at 21:43
  • @acampbe222 Check post below and comment under there. let me know, if its not gonna help you. – 7urkm3n Apr 03 '16 at 22:07
  • 7ukm3n this was the error message I got: ` heroku run rake db:migrate Running rake db:migrate on ouialex.... up, run.4912 rake aborted! NameError: uninitialized constant User ` – acampbe222 Apr 03 '16 at 22:26

1 Answers1

0

First of all Move your gem pg and gem rails_12factor in :production group

group :production do
 gem 'pg' 
 gem 'rails_12factor'
end

locally run to update gemlock file bundle install. then push to git push heroku master.


Link: PG RESET

To drop the database, if you are using SHARED_DATABASE_URL:

heroku pg:reset DATABASE

after pg:reset

! WARNING: Destructive Action
! This command will affect the app: NAME OF YOUR HEROKU APP
! To proceed, type "NAME OF YOUR HEROKU APP" or re-run this command with --confirm NAME OF YOUR HEROKU APP

To recreate the database with nothing in it:

heroku run rake db:migrate

You can combine the last two into one action by executing this:

heroku run rake db:setup

Community
  • 1
  • 1
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • 1
    @7urk3n I followed all of your steps, and I still get rake aborted! NameError: uninitialized constant User – acampbe222 Apr 03 '16 at 22:17
  • did you reset it ? r u use ? lets move to locally now to development environment. run locally `rake db:migrate:reset`. Lets see what happens. – 7urkm3n Apr 03 '16 at 22:31
  • After rake db:migrate:reset , this is the ouput ` == 20160403200237 DeviseCreateUsers: migrating ================================ -- create_table(:users) -> 0.0401s -- add_index(:users, :email, {:unique=>true}) -> 0.0049s -- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0033s == 20160403200237 DeviseCreateUsers: migrated (0.0486s) ======================= == 20160403210102 AddPlaylistToUsers: migrating =============================== -- add_column(:users, :playlist, :text) -> 0.0010s == 20160403210102 AddPlaylistToUsers: migrated (0.0011s) ====================== ` – acampbe222 Apr 03 '16 at 22:40
  • @7urk3n Yep. I ran a heroku run rake reset and then a migrate – acampbe222 Apr 03 '16 at 22:47
  • did u get any errors locally ? also heroku not supporting ` heroku run rake reset` command, You have to run like this ` heroku pg:reset DATABASE` – 7urkm3n Apr 03 '16 at 22:48
  • No errors locally. It just get this uninitialized User error when I run rake migrate in heroku – acampbe222 Apr 03 '16 at 23:01
  • I dnt know why u cannot reset it, `heroku pg:reset DATABASE ` – 7urkm3n Apr 03 '16 at 23:05
  • When I ran heroku run rake db:setup I got this error. Does this make any sense? Running rake db:setup on ouialex.... up, run.1784 FATAL: permission denied for database "postgres" DETAIL: User does not have CONNECT privilege. – acampbe222 Apr 04 '16 at 00:03