gpt4 book ai didi

ruby-on-rails - 名称错误 : unitialized constant User for rake db:migrate in Heroku

转载 作者:行者123 更新时间:2023-11-29 13:53:32 25 4
gpt4 key购买 nike

我最近刚刚将我的应用程序部署到 Heroku,但是当我运行 Heroku run rake db:migrate 时,出现以下错误。

 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)

下面是我设计的创建用户表(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

下面是一个名为 (20160403200704_add_email_to_users.rb)

的添加列
class AddEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :playlist, :text
end
end

下面是用户模型(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

只有当我在 heroku 中运行 rake db:migrate 时,我才会收到这些错误。当我在本地运行时,即使在生产模式下使用 rails s -e production 也能顺利运行。我真的可以在这方面使用帮助。谢谢!

下面是我的 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

最佳答案

首先将你的 gem pggem rails_12factor 移动到 :production group

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

本地运行以更新 gemlock 文件 bundle install。然后推送到 git push heroku master


链接:PG RESET

要删除数据库,如果您使用的是 SHARED_DATABASE_URL:

heroku pg:reset DATABASE

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

要重新创建其中没有任何内容的数据库:

heroku run rake db:migrate

您可以通过执行以下操作将最后两个合并为一个操作:

heroku run rake db:setup

关于ruby-on-rails - 名称错误 : unitialized constant User for rake db:migrate in Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com