Rake 错误坚持我有一个用户角色是我的应用程序名称?

Rake error insist I have a user role that is my App name?

我刚刚制作了一个新的 rails 5.0.0 应用程序。在我决定为 Heroku 预编译资产之前,一切都很好:

RAILS_ENV=production bundle exec rake assets:precompile

然后我看到了这个错误。我不知道为什么它希望角色成为我的应用程序的名称?到目前为止,我一直在进行预编译。仅添加了两个迁移文件(如下所列)。

rake aborted!
    ActiveRecord::NoDatabaseError: FATAL:  role "MYAPPNAME" does not exist
    /Users/james/MyApps/MYAPPNAME/app/models/model.rb:6:in `get_percent_change'
    /Users/james/MyApps/MYAPPNAME/lib/tasks/trade_wave_tasks.rake:3:in `block in <top (required)>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:12:in `<class:Application>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:10:in `<module:MYAPPNAME>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:9:in `<top (required)>'
    /Users/james/MyApps/MYAPPNAME/Rakefile:4:in `require_relative'
    /Users/james/MyApps/MYAPPNAME/Rakefile:4:in `<top (required)>'
    /Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
    /Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
    PG::ConnectionBad: FATAL:  role "MYAPPNAME" does not exist
    /Users/james/MyApps/MYAPPNAME/app/models/model.rb:6:in `get_percent_change'
    /Users/james/MyApps/MYAPPNAME/lib/tasks/trade_wave_tasks.rake:3:in `block in <top (required)>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:12:in `<class:Application>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:10:in `<module:MYAPPNAME>'
    /Users/james/MyApps/MYAPPNAME/config/application.rb:9:in `<top (required)>'
    /Users/james/MyApps/MYAPPNAME/Rakefile:4:in `require_relative'
    /Users/james/MyApps/MYAPPNAME/Rakefile:4:in `<top (required)>'
    /Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
    /Users/james/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
    Tasks: TOP => get_percent_change
    (See full trace by running task with --trace)

最初,任何 rake 命令都会给我一个问题。我修复了这些问题,运行 所有 rake 命令都有效——除了这个! (ie, rake db:reset) 我也创建了 postgresql USER 和 ROLE,但它仍然说同样的错误。我重新启动 psql,然后 rake db:reset.

以下是迁移文件:

创建模型:

class CreateMyModel < ActiveRecord::Migration[5.0]
  def change
    create_my_model :models do |t|

      t.timestamps
    end
  end
end

添加了一个专栏:

class AddPercentChangeToMyModelTable < ActiveRecord::Migration[5.0]
  def change
    remove_column :my_models, :percent_change, :string
    add_column :my_models, :percent_change, :string
  end
end

将其更改为浮点数:

class ChangePercentChangeToFloatOnMyModelTable < ActiveRecord::Migration[5.0]
  def change
    add_column :my_models, :percent_change, :float, precision: 8
  end
end

我也在使用 gem 所以我有一份 rake 工作(位于 tasks/my_rake_job.rake)

desc 'get latest percent change'
task get_percent_change: :environment do
  MyModel.get_percent_change
end

这是我的架构:

  create_table "bots", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "mymodels", force: :cascade do |t|
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
    t.float    "percent_change"
  end

  create_table "tweets", force: :cascade do |t|
    t.string   "message"
    t.string   "string"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Config/database

# PostgreSQL. Versions 9.1 and up are supported.
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: MYAPP_development

test:
  <<: *default
  database: MYAPP_test

production:
  <<: *default
  database: MYAPP_production
  username: MYAPP
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

还有我的 gem 文件:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# 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.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem 'twitter'
gem 'figaro'
gem 'randumb'
gem 'whenever', :require => false
gem "stock_quote"

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# 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', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

在删除角色并重新使它成为我的 psql \du table 之后看起来像这样:

 ccap      | Superuser, Create DB                                       | {}

请告诉我调试所需的其他文件。

这个问题与我在 rake 中调用的方法有关

desc 'get latest percent change'
task get_percent_change: :environment do
  MyModel.get_percent_change
end

为了更详细地说明这一点,这个 rake 任务正在调用一个试图创建某些东西的模型方法。而不是创建 rake 称为模型方法的东西,该方法返回一个浮点数,然后 rake 作业做了这样的事情:

  current_percent = Model.get_percent_change
  new_data = Model.new
  new_data.percent_change = current_percent
  new_data.save

当推送到 heroku 时,因为我试图更改列名,这个解决方案非常有用: How do I change column type in Heroku?

class ModifyContacts < ActiveRecord::Migration
  def self.up
    rename_column :contacts, :date_entered, :date_entered_string
    add_column :contacts, :date_entered, :date

    Contact.reset_column_information
    Contact.find(:all).each { |contact| contact.update_attribute(:date_entered, contact.date_entered_string) }
    remove_column :contacts, :date_entered_string
  end
end