Question

During upgrade from v1.5.1.0 to v1.6.0 it is trying to drop a key but can't because of a foreign key.

relevant lines in update script

$installer->getConnection()->dropIndex(
    $installer->getTable('core/store'),
    'FK_STORE_WEBSITE'
);

I see in the code for lib/Varien/Db/Adapter/Pdo/Mysql.php

public function startSetup()
{
    $this->raw_query("SET SQL_MODE=''");
    $this->raw_query("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0");
    $this->raw_query("SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");

    return $this;
}

but obviously it doesn't work.

I placed $installer->getConnection()->raw_query("SET FOREIGN_KEY_CHECKS=0;"); right before the relevant query to alter the table, but it has no useful effect.

I then made and ran a script to drop all foreign keys from the magento database. I confirmed that the foreign key is gone before starting the upgrade, but after starting the upgrade the foreign keys get added back in and start causing problems again.

So how do I properly disable foreign key checks or prevent the foreign keys from being added back in?

Was it helpful?

Solution 2

Turns out it was all due to incompatibilities in the newer versions of mysql and php. I setup different systems with outdated software to handle each stage of the upgrade with corresponding versions of mysql, php, and magento, and the update went smoothly.

OTHER TIPS

Do you have access to MYSQL cmd-line on the Magento server? If yes, you should be able to globally disable foreign key checks during your upgrade, then re-enable when you are done.

  1. Open a command-line interface on the machine running magento. If you are on a Mac, open a new terminal window. If your magento is on a remote machine you can SSH to the box - from Mac you can use a terminal window, from PC you can use Putty.

  2. Once you are connected to the Magento server, open MYSQL cmd-line, you can use the database username and password from your Magento instance (in M1, info can be found app/etc/local.xml).

For example, if you have SSH access, the commands would be as follows:

[userX@magento-server userX]# mysql -u magentoDbUser -p
Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10013
Server version: 10.4.7-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| magento1          |
+--------------------+
4 rows in set (0.007 sec)

MariaDB [(none)]> use magento1;
Database changed

MariaDB [magento1]> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.000 sec)

...
PERFORM YOUR MAGENTO UPGRADES IN ANOTHER BROWSER WINDOW
...
WHEN DONE, RE-ENABLE FKEY CHECKS
....

MariaDB [magento1]> SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.000 sec)

MariaDB [magento1]> exit
Bye
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top