ruby-on-rails - 在 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 )
在这一点上,我不知道发生了什么,我不知道在哪里更深入地搜索。
我希望你能帮助我理解问题并找到解决方案!
谢谢!
解决方案
推荐阅读
- python - 如何将 240*240*4 的 np 数组打印或转换为 240*240*1?
- unity3d - Unity:将 3d 对象投影/绘制到 2d 平面上
- bash - Bash -c 参数传递给 find
- spring-boot - Spring Cloud Stream:@StreamListener 处理消息两次
- sql - 如果基于此脚本设置回滚事务,脚本是否会获取光标中的下一项?
- css - 为什么 Devextreme 网格溢出它的父容器,为什么它没有创建垂直滚动条?
- excel - excel验证自定义函数
- amazon-web-services - 使用 c4.4xlarge 实例(5GB 数据)在 AWS 上出现内存错误和死内核
- python - 用 keras、tensorflow 和 python 编写这种奇特的神经网络架构
- ansible - 无法在 jinja2 语法中的大括号内添加大括号