internetztube / craft-element-relations

Shows all relations of an element.
https://plugins.craftcms.com/element-relations
Other
2 stars 5 forks source link

Failed update 1.0.6 => 1.2.3 #6

Closed digason closed 2 years ago

digason commented 2 years ago

Migration fails when upgrading from 1.0.6 to 1.2.3. Running Craft Pro 3.7.30.1

`Database Exception: SQLSTATE[HY000]: General error: 1828 Cannot drop column 'siteId': needed in a foreign key constraint 'fk_hsuqnkfyhtjzftcimobzwkwtwqutqrjuloos' The SQL being executed was: ALTER TABLE elementrelations DROP COLUMN siteId

Migration: internetztube\elementRelations\migrations\m220104_164702_simplify_cache_strategy

Output:

drop column siteId from table {{%elementrelations}} ...Exception: SQLSTATE[HY000]: General error: 1828 Cannot drop column 'siteId': needed in a foreign key constraint 'fk_hsuqnkfyhtjzftcimobzwkwtwqutqrjuloos' The SQL being executed was: ALTER TABLE elementrelations DROP COLUMN siteId (vendor/yiisoft/yii2/db/Schema.php:676)

0 vendor/yiisoft/yii2/db/Command.php(1307): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE el...')

1 vendor/yiisoft/yii2/db/Command.php(1102): yii\db\Command->internalExecute('ALTER TABLE el...')

2 vendor/yiisoft/yii2/db/Migration.php(392): yii\db\Command->execute()

3 vendor/internetztube/craft-element-relations/src/migrations/m220104_164702_simplify_cache_strategy.php(13): yii\db\Migration->dropColumn('{{%elementrelat...', 'siteId')

4 vendor/craftcms/cms/src/db/Migration.php(52): internetztube\elementRelations\migrations\m220104_164702_simplify_cache_strategy->safeUp()

5 vendor/craftcms/cms/src/db/MigrationManager.php(232): craft\db\Migration->up(true)

6 vendor/craftcms/cms/src/db/MigrationManager.php(148): craft\db\MigrationManager->migrateUp(Object(internetztube\elementRelations\migrations\m220104_164702_simplify_cache_strategy))

7 vendor/craftcms/cms/src/services/Updates.php(250): craft\db\MigrationManager->up()

8 vendor/craftcms/cms/src/controllers/BaseUpdaterController.php(532): craft\services\Updates->runMigrations(Array)

9 vendor/craftcms/cms/src/controllers/UpdaterController.php(201): craft\controllers\BaseUpdaterController->runMigrations(Array, 'restore-db')

10 [internal function]: craft\controllers\UpdaterController->actionMigrate()

11 vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

12 vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)

13 vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('migrate', Array)

14 vendor/craftcms/cms/src/web/Application.php(287): yii\base\Module->runAction('updater/migrate', Array)

15 vendor/craftcms/cms/src/web/Application.php(684): craft\web\Application->runAction('updater/migrate')

16 vendor/craftcms/cms/src/web/Application.php(244): craft\web\Application->_processUpdateLogic(Object(craft\web\Request))

17 vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))

18 web/index.php(22): yii\base\Application->run()

19 {main}`

gbowne-quickbase commented 2 years ago

I had the same issue. It looks like it's not possible to run `$this->dropColumn($table, 'siteId') because there's an index associated with it. Sadly, because the index name is generated automatically, there's no quick way to find that one to drop it. I faced similar issue in some other plugin and ran something like this:

foreach (Craft::$app->db->getSchema()->findIndexes($table) as $name => $index) {
         $this->dropIndex($name, $table);
}

I'm not great with migrations, but hope that @internetztube can figure out how to incorporate this into the latest or next rev.