首页 > 解决方案 > 在 Rails 5.2 中检查/拯救 sql 查询引发和错误

问题描述

DeployedSkill 对象引用了一个 ValuesList 对象,该对象为用户的答案提供了一个有限的值域。一个附加的“过滤器”字段包含一个表达式,用于过滤更多域的值。Values 是 Value 类的实例,属于 ValuesList 对象。

该模型如下所示:

class DeployedSkill < Skill
  ### before filter
  before_validation :set_code

  # Custom validation of the filter attribute
  validate :values_filter
  validates :code, presence: true, uniqueness: {scope: :business_object_id, case_sensitive: false}, length: { maximum: 32 }

  belongs_to :template_skill, :class_name => "DefinedSkill", :foreign_key => "template_skill_id" # helps retrieving the skill template
  belongs_to :parent, :class_name => "DeployedObject", :foreign_key => "business_object_id"

  belongs_to :values_list, inverse_of: :deployed_skills


  ### private functions definitions
  private

  ### format code
  def set_code
    if self.code[0, 3] == "DV_"
      self.code = self.code[3 .. -1]
    end
    self.code = "#{code.gsub(/[^0-9A-Za-z]/, '_')}".upcase[0,32]
  end

  def values_filter
    begin
      puts "----- Test the filter -----"
      Value.where("#{filter}").first
    rescue
      puts "----- Filter test failed -----"
      self.errors.add(:filter, "is not correctly specified")
      puts self.errors.map {|error, message| [error, message]}.join(', ')
    end
  end
end

在保存更新的 DeployedSkill 之前,我验证过滤器表达式是否与数据库查询兼容。使用 ByeBug,我可以看到测试工作正常,并且当过滤器的语法不正确时,ActiveModel::Errors 列表中会添加一个错误:

#<ActiveModel::Errors:0x0000000012bf03a8 @base=#<DeployedSkill id: 638, playground_id: 0,
business_object_id: 632, code: "SAKO1_METHODE", blablabla ... , 
type: "DeployedSkill", filter: "'xxx'", global_identifier: nil, sort_code: "SAKO1_METHODE", 
physical_name: nil>, 
@messages={:filter=>["is not correctly specified"]}, 
@details={:filter=>[{:error=>"is not correctly specified"}]}>

控制台回应它:

----- Test the filter -----
  Value Load (10.2ms)  SELECT  "values".* FROM "values" WHERE (code like 'S%) ORDER BY "values"."id" ASC LIMIT $1  [["LIMIT", 1]]
----- Filter test failed -----
filter, is not correctly specified
   (0.8ms)  ROLLBACK

尽管如此,更新方法说明:

      if @deployed_skill.update_attributes(deployed_skill_params)

闯入错误:

PG::InFailedSqlTransaction: ERROR: current transaction was aborted, commands are ignored until the end of the transaction : 
SELECT exists( 
  SELECT * FROM pg_proc 
  WHERE proname = 'lower' AND proargtypes = ARRAY['character varying(255)'::regtype]::oidvector ) 
  OR exists( SELECT * FROM pg_proc INNER JOIN pg_cast ON ARRAY[casttarget]::oidvector = proargtypes 
  WHERE proname = 'lower' AND castsource = 'character varying(255)'::regtype ) 

在这一点上,我不知道发生了什么,我不知道在哪里更深入地搜索。

我希望你能帮助我理解问题并找到解决方案!

谢谢!

标签: ruby-on-railsactiverecord

解决方案


推荐阅读