Skip to main content

— Technical

Fixing the ElasticSuite setup:upgrade trigger error in Magento 2

9 October 2024 · 4 min read

magento elasticsuite database patch

This one surfaces reliably when you install the Smile ElasticSuite extension on a fresh Magento environment and run setup:upgrade. The process fails with something like:

SQLSTATE[42S02]: Base table or view not found: 1146 Table
'magento.smile_elasticsuitecatalog_search_query_product_position' doesn't exist

query was: CREATE TRIGGER trg_smile_elasticsuitecat_srch_qr_prd_position_after_insert
AFTER INSERT ON smile_elasticsuitecatalog_search_query_product_position FOR EACH ROW
BEGIN
  INSERT IGNORE INTO `catalogsearch_fulltext_cl` (`entity_id`) VALUES (NEW.`product_id`);
END

The error looks like a missing table, but that’s a symptom. The actual problem is sequencing: Magento’s upgrade process is trying to create a trigger on a table before the table itself has been created.

What’s happening

In a specific version of Magento’s UpgradeCommand, the removeUnusedTriggers() call runs before installSchema(). That ordering means Magento attempts to create (or validate) triggers referencing tables that don’t exist yet because the schema patches haven’t run yet.

ElasticSuite creates smile_elasticsuitecatalog_search_query_product_position during its schema installation. If Magento tries to wire up triggers before that table exists, the trigger creation fails and the whole upgrade halts.

The fix

This is a known Magento core bug, tracked in PR #33092. The fix swaps two lines in UpgradeCommand.php:

- $installer->removeUnusedTriggers();
  $installer->installSchema($request);
+ $installer->removeUnusedTriggers();

Schema first. Triggers second. The fix is one line moved.

Since you can’t (and shouldn’t) edit core files directly, apply this as a Composer patch. Create the patch file at patches/magento/magento2-base/fix-upgrade-trigger-order.patch:

--- a/magento/magento2-base/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php
+++ b/magento/magento2-base/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php
@@ -142,8 +142,8 @@
         $searchConfig = $this->searchConfigFactory->create();
         $this->cache->clean();
         $searchConfig->validateSearchEngine();
-        $installer->removeUnusedTriggers();
         $installer->installSchema($request);
+        $installer->removeUnusedTriggers();
         $installer->installDataFixtures($request, true);

Register it in composer.json under the cweagans/composer-patches configuration:

"extra": {
    "patches": {
        "magento/magento2-base": {
            "Fix setup:upgrade trigger ordering before schema install": "patches/magento/magento2-base/fix-upgrade-trigger-order.patch"
        }
    }
}

Run composer install with the patch applied, then retry setup:upgrade. It will complete cleanly.

A note on patch path accuracy

If composer install throws a Argument #1 ($originUrl) must be of type string, null given error from cweagans/composer-patches, the patch path in your composer.json is wrong — either a typo in the directory name or the file genuinely doesn’t exist at that path. Double-check the path string character by character. This is a common source of confusion because the error message points at the patches library rather than your config.

Does this affect all ElasticSuite versions?

The trigger ordering issue exists in a range of Magento 2.4.x versions. If you’re on a version that already includes the fix from PR #33092, you won’t see this error. Check your Magento version against the PR’s merged state before applying the patch — applying it twice won’t cause problems, but it’s worth knowing whether it’s already included.

On Adobe Commerce Cloud, patches are managed through the ece-tools patch mechanism rather than cweagans/composer-patches — the same diff applies, just registered differently.

Savan Padaliya

Savan Padaliya

Senior Engineering Consultant

← Back to writing