简体   繁体   中英

Laravel/Postgres db queries?

I have (weird) problem with query on Laravel/Postgres DB Queries. I will paste entire query here(it's little too long but we will focus on just one part of it):

select distinct "products".*, "brands"."name" as "brand", "brands"."image" as "brand_image", "product_categories"."name" as "category", "product_categories"."slug" as "category_slug", count(distinct product_features.feature_category_id) as features_count from "products" inner join "brands" on "products"."brand_id" = "brands"."id" inner join "product_categories" on "products"."category_id" = "product_categories"."id" left join "product_features" on "products"."id" = "product_features"."product_id" where "products"."deleted_at" is null and "products"."category_id" in (142) and "products"."published" = true and "brands"."published" = true and "product_categories"."published" = true and "product_categories"."deleted_at" is null and "price" >= 0 and "price" <= 19000 and "products"."brand_id" in (17) and 
            (product_features.feature_category_id = 555 and
            CAST(product_features.value AS BOOLEAN) = true and
            product_features.deleted_at IS NULL) or
            (product_features.feature_category_id = 554 and
            CAST(product_features.value AS BOOLEAN) = true and
            product_features.deleted_at IS NULL) group by "products"."id", "brands"."name", "brands"."image", "product_categories"."name", "product_categories"."slug" having count(distinct product_features.feature_category_id) = 2 order by price asc

This is initial query but when I execute it I get weird error said:

ERROR:  invalid input syntax for type boolean: "800"

I have no idea why is this error thrown?

Your product_features.value is a text field. While Postgres is OK casting all nonzero integers like 800 to boolean true , it is not OK with casting text like '800' to boolean .

Solution is to check product_features.value on emptiness and/or equality to '0' instead of CAST(product_features.value AS BOOLEAN) = true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM