Heroku 上的 Rails 4 应用程序出现随机 ActiveRecord::StatementInvalid 错误

问题描述 投票:0回答:2

我的 Rails 4 应用程序在本地运行良好,并且当它以生产模式部署到 Heroku 时...直到我从第二个客户端访问该应用程序...然后单击几下后,我开始收到以下奇怪的错误序列(见下文)。

确切的点击次数略有不同,但最终总是会发生......而且只有在之后来自第二个客户的请求。例如,如果我重新启动应用程序并仅在手机上使用它,则不会发生任何情况。但是当我从笔记本电脑加载几页时......崩溃

错误

注意:这些屏幕截图带有

RAILS_ENV=development
,以便显示实际错误。

我不确定我是否正确理解这些错误,但看起来

?
在查询中没有被正确替换。

环境详情

  • Rails 4.2
  • 托管在 Heroku 上
  • 红宝石 2.2.x
  • MySQL(通过 ClearDB 插件)

我的宝石文件:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# 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/sstephenson/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', '~> 2.3.0'
# 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
gem 'devise'
gem "paperclip"
gem 'aws-sdk'
gem 'dotenv-rails', :groups => [:development, :test]
gem 'foundation-rails'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0', group: :development
gem 'mysql'
gem 'rails_12factor', group: :production
gem 'puma'
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'    
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

这是我的 Procfile:

web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

这是我的数据库.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  url: <%= ENV['DATABASE_URL'] %>

这是 config/生产.rb:

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like
  # NGINX, varnish or squid.
  # config.action_dispatch.rack_cache = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = 'http://assets.example.com'

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false

  config.serve_static_files = true

  config.paperclip_defaults = {
    storage: :s3,
    s3_credentials: {
      bucket: ENV.fetch('S3_BUCKET'),
      access_key_id: ENV.fetch('ACCESS_KEY_ID'),
      secret_access_key: ENV.fetch('SECRET_ACCESS_KEY'),
      s3_region: ENV.fetch('S3_REGION'),
      s3_host_name: 's3-us-west-2.amazonaws.com'
    }
  }


end

我尝试过的事情:

  • 重新启动所有测功机(工作一分钟,但问题又回来了)
  • 切换到
    gem mysql2
    (甚至无法运行)
    请参阅下面的答案
  • 切换到
    gem puma
    (遵循 本文档
  • 使用客户端连接到数据库(一切似乎工作正常)
  • 确认请求的参数是正确的这个问题(请求参数看起来不错)
  • 多次阅读此 Heroku / Rails 4 文档
类似的问题没有解决我的问题

  • 在heroku上插入数据库错误,但在本地工作,ActiveRecord::StatementInvalid
  • Heroku + Rails 错误 ActiveRecord::StatementInvalid
  • Heroku 上不断出现 500 个错误
ruby-on-rails ruby ruby-on-rails-4 heroku activerecord
2个回答
2
投票
我第二次尝试使用 mysql2 gem 进行部署,这次成功了。秘密武器来自

这个问题

TLDR; 您需要指定 mysql2 的旧版本,以便与 Rails 的

较新版本兼容。是的,这很奇怪。

gem 'mysql2', '~> 0.3.18'

使用此 gem 进行部署后,我无法再重现该问题。我不明白,但它有效。

如果有人提交的答案可以解释发生了什么,我会接受。


0
投票
遇到类似的错误,我的解决方案是更新我的控制器:

之前:

def index @participants = @events = Participant.all // before update format.html { render html: @participants } render json: { participants: @participants } end

之后:更新了

def index @participants = @events = Api::V1::Participant.all format.html { render html: @participants } render json: { participants: @participants } end
然后我在模型中将表格设置为 self :

class Api::V1::Participant < ApplicationRecord self.table_name = 'participants' //add this to your model belongs_to :event end
    
© www.soinside.com 2019 - 2024. All rights reserved.