cancel
Showing results for 
Search instead for 
Did you mean: 

404 page only when user is logged in (SQLSTATE[23000]: Integrity constraint violation: 1452)

404 page only when user is logged in (SQLSTATE[23000]: Integrity constraint violation: 1452)

Whenever a logged in user tries to do something related to his account, then he is redirected to 404 page. Below, I give 4 cases that the error occurs.


Case 1. User tries to login, the page is blank, then if you refresh the user account area appears with a message "We can't show you the wishlist now".
Case 2. User is logged in and tries to visit a product page, then he is redirected to the 404 page.
Case 3. User tries to add a product to cart, then he is redirected to the 404 page.
Case 4: Visitor (not logged in) orders a product and at the success page (after successfully submitting the order) decides to create an account (the system offers it with a button), then the same as Case 1 happens.

I upgraded from Magento 2.2.0 to 2.2.5, but the problem is still there.

 

Preconditions

  1. Magento Version 2.2.0 and 2.2.5 (after update).
  2. php 7.0.13 on apache and nginx / debian 9.
  3. One Website, with Two stores and Two Store Views (one for each store).

Steps to reproduce

In order to reproduce one has to login first or try to visit a product page so that he can face this 404 error.

 

Expected result

  1. The logged in user should be able to visit a product's page.
  2. The visitor should be able to login normally without error.

Actual result

  1. The logged in user was redirected to the 404 page (the product link on the browser is the correct one).
  2. The visitor saw a blank page, then had to refresh in order to view his account page.

Here is an exception from exception.log file:

[2018-10-07 10:26:49] main.CRITICAL: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mysitedb.report_viewed_product_index, CONSTRAINT REPORT_VIEWED_PRD_IDX_CSTR_ID_CSTR_ENTT_ENTT_ID FOREIGN KEY (customer_id) REFERENCES customer_entity_renamed (entity_id) ON D), query was: INSERT INTO report_viewed_product_index(customer_id,product_id,store_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE customer_id = VALUES(customer_id), product_id = VALUES(product_id), store_id = VALUES(store_id) {"exception":"[object] (Zend_Db_Statement_Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mysitedb.report_viewed_product_index, CONSTRAINT REPORT_VIEWED_PRD_IDX_CSTR_ID_CSTR_ENTT_ENTT_ID FOREIGN KEY (customer_id) REFERENCES customer_entity_renamed (entity_id) ON D), query was: INSERT INTO report_viewed_product_index(customer_id,product_id,store_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE customer_id = VALUES(customer_id), product_id = VALUES(product_id), store_id = VALUES(store_id) at /home/mysite/domains/mysitedb.mysite.com/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:235, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mysitedb.report_viewed_product_index, CONSTRAINT REPORT_VIEWED_PRD_IDX_CSTR_ID_CSTR_ENTT_ENTT_IDFOREIGN KEY (customer_id) REFERENCES customer_entity_renamed (entity_id) ON D) at /home/mysite/domains/mysitedb.mysite.com/public_html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228)"} []

3 REPLIES 3

Re: 404 page only when user is logged in (SQLSTATE[23000]: Integrity constraint violation: 1452)

Hello @nicmedlabs,

 

Please run below query in mysql 

TRUNCATE TABLE report_viewed_product_index;

After running above query please try to replicate this issue. 

 

--
If my answer is useful, please Accept as Solution & give Kudos

Re: 404 page only when user is logged in (SQLSTATE[23000]: Integrity constraint violation: 1452)

Already done this, with no luck.

 

Something seems to mess with the session, because this 404 occurs only when the user is logged in.

 

Can this happen because of database entries that shouldn't be there?

 

Also, when I am in production mode, I receive the following error in the Apache error logs:

 

[Mon Oct 08 10:21:26.586003 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: PHP Fatal error: Uncaught Error: Call to a member function getItemCollection() on boolean in /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/Helper/Data.php:233, referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586020 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: Stack trace:, referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586023 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: #0 /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/Helper/Data.php(244): Magento\\Wishlist\\Helper\\Data->_createWishlistItemCollection(), referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586025 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: #1 /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/Helper/Data.php(565): Magento\\Wishlist\\Helper\\Data->getWishlistItemCollection(), referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586027 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: #2 /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/Helper/Data.php(220): Magento\\Wishlist\\Helper\\Data->calculate(), referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586029 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: #3 /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/CustomerData/Wishlist.php(75): Magento\\Wishlist\\Helper\\Data->getItemCount(), referer: https://mysite.com/gant-man-cardigan-105398.html
[Mon Oct 08 10:21:26.586031 2018] [fcgid:warn] [pid 16123] [client 212.205.95.220:44724] mod_fcgid: stderr: #4 /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/CustomerData/Wishlist.php(63): Magento\\Wishlist\\Customer in /home/mysiteFolder/mysite.com/public_html/vendor/magento/module-wishlist/Helper/Data.php on line 233, referer: https://mysite.com/gant-man-cardigan-105398.html

Re: 404 page only when user is logged in (SQLSTATE[23000]: Integrity constraint violation: 1452)

Hello,

i have a new installed Magento 2.2.6 and when i was creating a new customer account in the frontend, it ends with the following:

Fatal error: Uncaught Error: Call to a member function getItemCollection() on boolean in /html/wilma/vendor/magento/module-wishlist/Helper/Data.php:233

Stack trace:

#0 /html/wilma/vendor/magento/module-wishlist/Helper/Data.php(244): Magento\Wishlist\Helper\Data->_createWishlistItemCollection()

#1 /html/wilma/vendor/magento/module-wishlist/Helper/Data.php(565): Magento\Wishlist\Helper\Data->getWishlistItemCollection()

#2 /html/wilma/vendor/magento/module-wishlist/Observer/CustomerLogin.php(38): Magento\Wishlist\Helper\Data->calculate()

#3 /html/wilma/vendor/magento/framework/Event/Invoker/InvokerDefault.php(72): Magento\Wishlist\Observer\CustomerLogin->execute(Object(Magento\Framework\Event\Observer))

#4 /html/wilma/vendor/magento/framework/Event/Invoker/InvokerDefault.php(60): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Magento\Wishlist\Observer\CustomerLogin), Object(Magento\Framework\Event\Observer))

#5 /html/wilma/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Inv in /html/wilma/vendor/magento/module-wishlist/Helper/Data.php on line 233

 

If i refresh the site the following system message is shown: "We can't create the Wish List right now."

 

The system created the new customer account and the welcome mail will also be send. 

 

What could i do?