Magento2 - debug SQL errors - magento2

I am getting errors like
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.name' in 'field list', query was: SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `cat_index`.`position` AS `cat_index_position`, `e`.`name`, `e`.`image`, `e`.`small_image`, `e`.`thumbnail`, `e`.`name`, `e`.`short_description`, `e`.`price`, `e`.`special_price`, `e`.`special_from_date`, `e`.`special_to_date`, `e`.`image`, `e`.`small_image`, `e`.`thumbnail`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`required_options`, `e`.`image_label`, `e`.`small_image_label`, `e`.`thumbnail_label`, `e`.`url_key`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`price_type`, `e`.`weight_type`, `e`.`price_view`, `e`.`links_purchased_separately`, `e`.`links_exist`, `e`.`swatch_image`, `e`.`tax_class_id`, `e`.`cross_tier_id`, `e`.`cross_tier_id_value`, `e`.`open_amount_max`, `e`.`open_amount_min`, `e`.`package_size`, `e`.`sw_featured`, SUM(soi.qty_ordered) AS `ordered_qty`, `order`.`state`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0
INNER JOIN `catalog_category_product_index_store1` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id=2
LEFT JOIN `sales_order_item` AS `soi` ON soi.product_id = e.entity_id
INNER JOIN `sales_order` AS `order` ON order.entity_id = soi.order_id
LEFT JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 0 AND stock_status_index.stock_id = 1 WHERE (order.state <> 'canceled' and soi.parent_item_id IS NULL AND soi.product_id IS NOT NULL) GROUP BY `soi`.`product_id` ORDER BY `ordered_qty` DESC
LIMIT 8
In magento logs i do not see information which class produced this error.
How can i found out what class/module is responsible for this query?

you can run bin / magento dev: query-log: enable
to see all sql queries

Related

Postgresql: Error missing FROM-clause entry for table

I have a query and I keep getting this a 'missing FROM-clase entry for table e, rx.rootentityid = rh.entityid1 AND rh.schoolid = e.schoolid'.
can anyone see the error in my code. My postgresql knowledge is very limited.
SELECT
eh.entityid1, eh.entityname1, eh.entityid2, eh.entityname2,
eh.entityid3, eh.entityname3, eh.entityid4, eh.entityname4,
eh.entityid5, eh.entityname5, eh.entityid6, eh.entityname6,
eh.entityid7, eh.entityname7, eh.schoolentityid, eh.schoolid,
eh.emiscode, eh.schoolname, rh.entityid1 AS reportentityid1,
rh.entityname1 AS reportentityname1, rh.entityid2 AS reportentityid2,
rh.entityname2 AS reportentityname2
FROM dbo.vwentityhierarchyname
JOIN dbo.entity AS eh
ON vwentityhierarchyname.entityid1 = eh.entityid AND eh.entitytypeid = 1
CROSS JOIN (SELECT
hd.rootentityid
FROM dbo.hierarchydefinition AS hd
JOIN dbo.entity AS e
ON hd.rootentityid = e.entityid
WHERE hd.relatedrootentityid = e.entityid AND hd.entitytypeid = 7 AND LOWER(e.name) SIMILAR TO LOWER('%Report Regions%')) AS rx
LEFT OUTER JOIN dbo.vwentityhierarchyname AS rh
ON rx.rootentityid = rh.entityid1 AND rh.schoolid = e.schoolid
WHERE eh.schoolid = par_SchoolID
ORDER BY eh.entityid1 DESC NULLS FIRST
LIMIT 1;
Thanks

use alias name as parameter

how to i get alias name for my query. Below i am using spring boot with hibernate JPA native query. What i want is to get alias name cbpartnerid as WHERE parameter, because i get c_b_partner from 2 table with condition.
But spring giver me error this :
ERROR 2021-02-26 06:00:02.492 [http-nio-8080-exec-9] o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions(142) - ERROR: column "cbpartnerid" does not exist
i hope can solve this issue without change overall query or modify backend code.
here are my native query :
(CASE
WHEN i.transaction = 'SALES' OR i.transaction = 'CUSTOMER_RETURN' THEN j.c_bpartner_id
WHEN i.transaction = 'INVENTORY_OUT' OR i.transaction = 'INVENTORY_OUT' THEN l.c_bpartner_id
ELSE null
END) AS **cbpartnerid**
FROM so_transaction i
LEFT JOIN so_orderline j ON j.so_orderline_id = i.so_orderline_id LEFT JOIN so_inventoryline k ON k.so_inventoryline_id = i.so_inventoryline_id
LEFT JOIN so_inventory l ON l.so_inventory_id = k.so_inventory_id
GROUP BY i.created, i.transaction, i.m_product_id, i.productname, i.createdby, **cbpartnerid** ORDER BY i.m_product_id DESC ```
The easy solution is a subquery.
Your code, reduced to the essential, looks like
SELECT /* complicated expression */ AS alias,
/* other columns */
FROM atable
GROUP BY alias;
This can be rewritten to
SELECT alias, /* other columns */
FROM (SELECT /* complicated expression */ AS alias,
/* other columns */
FROM atable) AS subq
GROUP BY alias;
SELECT i.created, i.transaction, i.m_product_id, i.productname, i.createdby,
(CASE
WHEN i.transaction = 'SALES' OR i.transaction = 'CUSTOMER_RETURN' THEN j.c_bpartner_id
WHEN i.transaction = 'INVENTORY_OUT' OR i.transaction = 'INVENTORY_OUT' THEN l.c_bpartner_id
ELSE null
END) AS **cbpartnerid**
FROM so_transaction i
LEFT JOIN so_orderline j ON j.so_orderline_id = i.so_orderline_id LEFT JOIN so_inventoryline k ON k.so_inventoryline_id = i.so_inventoryline_id
LEFT JOIN so_inventory l ON l.so_inventory_id = k.so_inventory_id
GROUP BY i.created, i.transaction, i.m_product_id, i.productname, i.createdby, **cbpartnerid** ORDER BY i.m_product_id DESC ```

column reference id is ambiguous postgres

I'm writing a new query postgresql with alias but i still have the same problem of ambiguous column.
select a.id_application
from (SELECT * FROM t_mission as PM
LEFT JOIN t_mission_raf rm on PM.id_mission= rm.id_rm
LEFT JOIN t_mission_roles AS mr ON PM.id_mission = mr.id_mission
LEFT JOIN t_role AS r ON r.id_role = mr.id_role
LEFT JOIN t_appli AS app ON app.id_application = r.id_application
WHERE PM.type_mission = 1 AND PM.id_mission =24730) as a
Result:
Error msg : SQL Error [42702]: ERROR: column reference "id_application" is ambiguous
Position : 8
Don't SELECT * but exlipcitly select the columns you need. Make sure to only select app.id_application or r.id_application. Or use as for one of those.

Schema error from postgresql update statement

I have a POSTGRESQL update statement that I'm trying to rewrite with explicit joins and needing some help whereby the NESTED select statement alias is recognized to execute the outer UPDATE statement. Not sure why the "se" alias for the nested select is NOT recognized. I know I must be missing something basic.
Any help/direction would be appreciated. Thanks.
Error I'm getting:
ERROR: schema "se" does not exist
********** Error **********
ERROR: schema "se" does not exist
SQL state: 3F000
Here is my code:
UPDATE students
SET custom_400011449 = CAST(cfso1.label as integer)+1,
custom_400011770 = 761 --'2016-2017'
FROM (
SELECT se2.student_id, cfso1.id, cfso1.label
FROM students s
INNER JOIN student_enrollment se2
ON s.student_id = se2.student_id
INNER JOIN school_gradelevels sg
ON se2.grade_id = sg.id
INNER JOIN custom_field_log_entries sle
ON se2.student_id = sle.source_id
LEFT JOIN custom_field_select_options cfso1
ON s.custom_400011449 = cfso1.id
LEFT JOIN custom_field_select_options cfso2
ON s.custom_400011770 = cfso2.id
LEFT JOIN custom_field_select_options cfso3
ON sle.log_field4::INT = cfso3.id
WHERE se2.syear =2016
AND s.custom_400011449 IS NOT NULL
AND s.custom_400011898 IS NULL
AND s.custom_400011899 IS NULL
AND sle.field_id = 15 --400010549
AND sle.log_field2 IS NULL
AND cfso2.label = '2015-2016'
AND cfso3.label IN ('LE', 'NL-F', 'NL-S')
AND sg.short_name BETWEEN '01' AND '12'
AND (
SELECT COUNT(school_date)
FROM student_enrollment se
INNER JOIN attendance_calendar ac
ON se.calendar_id = ac.calendar_id
WHERE se.student_id = se2.student_id
AND se.syear =2016 --{SYEAR}
AND ac.syear =2016 --{SYEAR}
AND ac.school_date >= se.start_date
AND ac.school_date <= NOW()
AND (se.end_date IS NULL OR ac.school_date <= se.end_date)
AND se.custom_9 IS NULL
AND se2.start_date >= '2016-08-22'
GROUP BY se.student_id
HAVING COUNT(ac.school_date) >= 59)
>= 59) AS se
LEFT JOIN custom_field_select_options cfso1
ON se.cfso1.id = cfso1.id
WHERE students.student_id = se.student_id
I found my problem whereby I had a syntax error in the ON condition "se.cfso1.id". Once I cleared that up the problem was resolved.

Postgres Complex Select in a View

I have this select clause that is working perfect:
SELECT
"Aspectos"."ID" AS "Aspecto Normativo ID",
"Aspectos"."Aspecto" AS "Aspecto Normativo",
"Fatores"."ID", "Fatores"."Fator" AS "Fator Normativo",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator" AS "Fator Determinativo",
"Aspectos_1"."ID" AS "Aspecto Determinativo ID",
"Aspectos_1"."Aspecto" AS "Aspecto Determinativo",
Count("Itens"."ID") AS "No Itens",
Count("Itens"."ID") AS "Pri"
FROM "Diagnostico" INNER JOIN ("Aspectos" AS "Aspectos_1"
INNER JOIN (("Fontes" INNER JOIN "Itens" ON "Fontes"."ID" = "Itens"."Fonte")
INNER JOIN ("Fatores" AS "Fatores_1"
INNER JOIN ("Aspectos"
INNER JOIN ("Vinculos"
INNER JOIN "Fatores"
ON "Vinculos"."Fator_Normativo" = "Fatores"."ID")
ON ("Aspectos"."ID" = "Fatores"."Aspecto")
AND ("Aspectos"."ID" = "Fatores"."Aspecto"))
ON "Fatores_1"."ID" = "Vinculos"."Fator_Determinativo")
ON "Itens"."ID" = "Vinculos"."Item")
ON "Aspectos_1"."ID" = "Fatores_1"."Aspecto")
ON "Diagnostico"."ID" = "Vinculos"."Diagnostico_ID"
GROUP BY "Aspectos"."ID", "Aspectos"."Aspecto",
"Fatores"."ID", "Fatores"."Fator",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator",
"Aspectos_1"."ID",
"Aspectos_1"."Aspecto"
ORDER BY "Aspectos"."ID", "Aspectos_1"."ID",
"Fatores"."Fator", "Fatores_1"."Fator";
But when I try to CREATE A VIEW with this same select I'm getting thuis error:
ERROR: column "ID" specified more than one time
Can anybody help me on this.
Thanks
You have "Fatores"."ID" (line 4) and "Fatores_1"."ID" (line 6). Give them different aliases.
For such complex queries it is recommended to have only 1 (one) column in per line in the statement for better visibility. Also it is recommended to always give aliases to the columns.