Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

mysql - ArgumentError (hour out of range) in ActiveRecord

I have a problem with a legacy MySQL database that I'm not able to solve. I believe the culprit is that one of the fields has the wrong type set (it contains time in milliseconds, which the adapter doesn't expect having type DateTime), and unfortunately I can't change it. What happens is this (using Ruby on Jets):

2.5.8 :001 > User.all.each {|u| puts u.inspect};mil
Traceback (most recent call last):
       16: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/relation.rb:250:in `records'
       15: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/relation.rb:626:in `load'
       14: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/relation.rb:808:in `exec_queries'
       13: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/relation.rb:839:in `skip_query_cache_if_necessary'
       12: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/relation.rb:821:in `block in exec_queries'
       11: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/querying.rb:46:in `find_by_sql'
       10: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:12:in `select_all'
        9: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:107:in `select_all'
        8: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:70:in `select_all'
        7: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:489:in `select'
        6: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:46:in `exec_query'
        5: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:210:in `execute_and_free'
        4: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:48:in `block in exec_query'
        3: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:48:in `to_a'
        2: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:48:in `each'
        1: from /home/ngw/.rvm/gems/ruby-2.5.8/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql/database_statements.rb:48:in `utc'
ArgumentError (hour out of range)

I've tried to monkey patch this but I don't think I'm patching the right thing, it looks like the thing to change is the adapter (ouch...). I've seen other changed ActiveSupport but it didn't work out for me, and basically I can't loop over my records without ActiveRecord crashing. I can't even rescue the problem, I don't know why but rescue ArgumentError doesn't get triggered by that exception. Has anyone encountered this problem and managed to disable AR parsing of the records? It would be enough to get the value that crashes AR and / 1000 but I apparently can't find a way to do this.

Model:

class User < ApplicationRecord
  self.table_name = :user
  self.primary_key = :user_id

  alias_attribute :first_name, 'user_firstname'
  alias_attribute :last_name, 'user_lastname'
  alias_attribute :email, 'user_email'
  alias_attribute :phone, 'user_contact_number'
  alias_attribute :job_title, 'user_job_title'
  alias_attribute :contact_status, 'user_contact_status'
  alias_attribute :created_at, 'user_created'

  # has_one :settings, class_name: 'UserSettings'
  # has_many :cases, foreign_key: :created_by_user_id
  # has_many :subscriptions, -> { order('id DESC') }
  # has_many :orders, foreign_key: :user_id
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...