{"id":199,"date":"2021-06-09T14:04:57","date_gmt":"2021-06-09T14:04:57","guid":{"rendered":"https:\/\/magebrew.com\/blog\/?p=199"},"modified":"2021-06-09T14:04:57","modified_gmt":"2021-06-09T14:04:57","slug":"how-to-debug-magento-2-mysql-sqlstate-errors","status":"publish","type":"post","link":"https:\/\/magebrew.com\/blog\/how-to-debug-magento-2-mysql-sqlstate-errors\/","title":{"rendered":"How to debug Magento 2 MySQL (SQLSTATE) errors"},"content":{"rendered":"\n<p>Sometimes it is not easy to figure out where the database related errors come from. Error is usually being written to <em>exception.log<\/em> by <em>Magento\\Framework\\DB\\Statement\\Pdo\\Mysql<\/em> class. But error message is not giving you idea what exactly caused the error.  Below is the trick to get more info about error.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Recently on one of the projects we have got the following error and it was not obvious what triggered it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row<\/pre>\n\n\n\n<p>Error is generated in <em>vendor\/magento\/framework\/DB\/Statement\/Pdo\/Mysql.php<\/em> in <em>tryExecute<\/em> method which looks like that:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private function tryExecute($callback)\n    {\n        $previousLevel = error_reporting(\\E_ERROR); \/\/ disable warnings for PDO bugs #63812, #74401\n        try {\n            return $callback();\n        } catch (\\PDOException $e) {\n            $message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString);\n            throw new \\Zend_Db_Statement_Exception($message, (int)$e->getCode(), $e);\n        } finally {\n            error_reporting($previousLevel);\n        }\n    }<\/pre>\n\n\n\n<p>Let&#8217;s modify it and add backtrace info to the message. This way we will know what feature or module triggers the error.  Here is how it can look like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> private function tryExecute($callback)\n    {\n        $previousLevel = error_reporting(\\E_ERROR); \/\/ disable warnings for PDO bugs #63812, #74401\n        try {\n            return $callback();\n        } catch (\\PDOException $e) {\n            $info = 'info:';\n            foreach (debug_backtrace() as $_stack) {\n                $info .=  (isset($_stack[\"file\"]) ? $_stack[\"file\"] : '') . ':' .\n                    (isset($_stack[\"line\"]) ? $_stack[\"line\"] : '') . ' - ' .\n                    (isset($_stack[\"function\"]) ? $_stack[\"function\"] : '') . PHP_EOL;\n            }\n\n            $r = new \\ReflectionFunction($callback);\n            $static = $r->getStaticVariables();\n            if (isset($static['params'])) {\n                $message = sprintf('%s, query was: %s, parameters: %s, backtrace: %s', $e->getMessage(), $this->_stmt->queryString, json_encode($static['params']), $info);\n            } else {\n                $message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString);\n            }\n\n            throw new \\Zend_Db_Statement_Exception($message, (int)$e->getCode(), $e);\n        } finally {\n            error_reporting($previousLevel);\n        }\n    }<\/pre>\n\n\n\n<p>Hope it will save you some time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is not easy to figure out where the database related errors come from. Error is usually being written to exception.log by Magento\\Framework\\DB\\Statement\\Pdo\\Mysql class. But error message is not giving you idea what exactly caused the error. Below is the trick to get more info about error.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[20],"_links":{"self":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/199"}],"collection":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":2,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/199\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}