轨中0的CAST的怪异行为

时间:2018-12-02 19:37:14

标签: ruby-on-rails postgresql

很抱歉,很长的查询时间,但是我正在使用它。 我在Rails中得到了这个查询

@posts = Cama::PostType.first.posts.joins(:custom_field_values).where("(status = ?) AND (cama_custom_fields_relationships.custom_field_slug = ? AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) >= ? AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) <= ? ) OR 
(cama_custom_fields_relationships.custom_field_slug = ? AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) >= ? AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) <= ? ) AND 
(LOWER(title) LIKE ? OR LOWER(content_filtered) LIKE ?)","published", "filtry-
powierzchnia", params['size-start'].to_i, params['size-end'].to_i,"filtry-
cena", params['price-start'].to_i, params['price-end'].to_i 
,"%#{params[:q]}%", 
"%#{params[:q]}%").group("cama_posts.id").having("COUNT(cama_custom_fields_rel
ationships.objectid) >= 2") 

使此sql成为

CamaleonCms::Post Load (0.9ms)  SELECT  "cama_posts".* FROM "cama_posts"
 INNER JOIN "cama_custom_fields_relationships" ON 
"cama_custom_fields_relationships"."objectid" = "cama_posts"."id" AND 
"cama_custom_fields_relationships"."object_class" = $1 WHERE 
"cama_posts"."post_class" = $2 AND "cama_posts"."taxonomy_id" = $3 AND 
((status = 'published') AND 
(cama_custom_fields_relationships.custom_field_slug = 'filtry-powierzchnia'
AND CAST(cama_custom_fields_relationships.value AS INTEGER) >= 1 AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000 ) OR 
(cama_custom_fields_relationships.custom_field_slug = 'filtry-cena' AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) >= 0 AND 
CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000000 ) AND
(LOWER(title) LIKE '%%' OR LOWER(content_filtered) LIKE '%%')) GROUP BY 
cama_posts.id HAVING (COUNT(cama_custom_fields_relationships.objectid) >= 2)
 ORDER BY "cama_posts"."post_order" ASC, "cama_posts"."created_at" DESC LIMIT
 $4 OFFSET $5  [["object_class", "Post"], ["post_class", "Post"], 
["taxonomy_id", 2], ["LIMIT", 6], ["OFFSET", 0]]

如您所见,我正在使用CAST,我不能仅将列更改为整数,因为它是CMS。我创建了custom_field,它应该是整数,但是db中的表仍然是文本,因此我被迫这样做。

如果我的

现在一切都很好
..CAST(cama_custom_fields_relationships.value AS INTEGER) >= 1..

,但是当该值为0时 所以当我的查询看起来像

CamaleonCms::Post Load (0.9ms)  SELECT  "cama_posts".* FROM "cama_posts"
     INNER JOIN "cama_custom_fields_relationships" ON 
    "cama_custom_fields_relationships"."objectid" = "cama_posts"."id" AND 
    "cama_custom_fields_relationships"."object_class" = $1 WHERE 
    "cama_posts"."post_class" = $2 AND "cama_posts"."taxonomy_id" = $3 AND 
    ((status = 'published') AND 
    (cama_custom_fields_relationships.custom_field_slug = 'filtry-powierzchnia'
    AND ****CAST(cama_custom_fields_relationships.value AS INTEGER) >= 0**** AND 
    CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000 ) OR 
    (cama_custom_fields_relationships.custom_field_slug = 'filtry-cena' AND 
    CAST(cama_custom_fields_relationships.value AS INTEGER) >= 0 AND 
    CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000000 ) AND
    (LOWER(title) LIKE '%%' OR LOWER(content_filtered) LIKE '%%')) GROUP BY 
    cama_posts.id HAVING (COUNT(cama_custom_fields_relationships.objectid) >= 2)
     ORDER BY "cama_posts"."post_order" ASC, "cama_posts"."created_at" DESC LIMIT
     $4 OFFSET $5  [["object_class", "Post"], ["post_class", "Post"], 
    ["taxonomy_id", 2], ["LIMIT", 6], ["OFFSET", 0]]

出现错误:

ActionView::Template::Error (PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "/media/1/asd.jpg"
: SELECT  "cama_posts".* FROM "cama_posts" INNER JOIN "cama_custom_fields_relationships" ON "cama_custom_fields_relationships"."objectid" = "cama_posts"."id" AND "cama_custom_fields_relationships"."object_class" = $1 WHERE "cama_posts"."post_class" = $2 AND "cama_posts"."taxonomy_id" = $3 AND ((status = 'published') AND (cama_custom_fields_relationships.custom_field_slug = 'filtry-powierzchnia' AND CAST(cama_custom_fields_relationships.value AS INTEGER) >= 0 AND CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000 ) OR (cama_custom_fields_relationships.custom_field_slug = 'filtry-cena' AND CAST(cama_custom_fields_relationships.value AS INTEGER) >= 0 AND CAST(cama_custom_fields_relationships.value AS INTEGER) <= 10000000 ) AND (LOWER(title) LIKE '%%' OR LOWER(content_filtered) LIKE '%%')) GROUP BY cama_posts.id HAVING (COUNT(cama_custom_fields_relationships.objectid) >= 2) ORDER BY "cama_posts"."post_order" ASC, "cama_posts"."created_at" DESC LIMIT $4 OFFSET $5):

和@posts在查询lvl上返回它,因此它不会在其他地方发生错误。

我尝试将CASTing转换为数字,同样的问题。它仅在其开始条件时发生,当它之前还有另一个条件时,它像我的示例一样接受0,此条件之后的第二个条件与> = 0

0 个答案:

没有答案