Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: Plugin table not found during site install #15506

Closed
janhenckens opened this issue Aug 8, 2024 · 7 comments
Closed

[5.x]: Plugin table not found during site install #15506

janhenckens opened this issue Aug 8, 2024 · 7 comments
Labels

Comments

@janhenckens
Copy link
Contributor

What happened?

Description

In my SEO plugin, I'm listening for Sites::EVENT_AFTER_SAVE_SITE to add default values in my now table for the new site. (here)

When installing the plugin on its own, that works - but when installing from existing project config files, that contain the plugin, this failed because the table can't be found - (yet?)

Exception: The table does not exist: {{%seofields_data}} (/var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php:442)
#0 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(266): yii\db\ActiveRecord::getTableSchema()
#1 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(238): yii\db\ActiveRecord::filterValidColumnNames(Object(craft\db\Connection), Array)
#2 /var/www/html/vendor/yiisoft/yii2/db/ActiveRecord.php(195): yii\db\ActiveRecord::filterCondition(Array, Array)
#3 /var/www/html/vendor/yiisoft/yii2/db/BaseActiveRecord.php(110): yii\db\ActiveRecord::findByCondition(Array)
#4 /var/www/html/vendor/studioespresso/craft-seo-fields/src/services/DefaultsService.php(115): yii\db\BaseActiveRecord::findOne(Array)
#5 /var/www/html/vendor/studioespresso/craft-seo-fields/src/services/DefaultsService.php(63): studioespresso\seofields\services\DefaultsService->getRecordForSiteId(NULL)
#6 /var/www/html/vendor/studioespresso/craft-seo-fields/src/services/DefaultsService.php(123): studioespresso\seofields\services\DefaultsService->getDataBySiteId(NULL)
#7 /var/www/html/vendor/studioespresso/craft-seo-fields/src/SeoFields.php(319): studioespresso\seofields\services\DefaultsService->copyDefaultsForSite(Object(craft\models\Site), NULL)

Checking the database I see that table doesn't exist yet, and in the plugins table I can see that the plugin isn't actually installed yet - I think.

I tried wrapping my event listeners in Craft::$app->onInit(function () {}); but the error still happens.

What would be the best way to fix this? Thanks!

Craft CMS version

5.3.0.3

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@janhenckens janhenckens added the bug label Aug 8, 2024
@janhenckens janhenckens changed the title [5.x]: Plugin table not found during install [5.x]: Plugin table not found during site install Aug 8, 2024
@brandonkelly
Copy link
Member

brandonkelly commented Aug 10, 2024

The problem is that your code is latching onto the EVENT_AFTER_SAVE_SITE event, which is triggered during install, before the plugin itself has been installed (and your tables have been created).

If you change this condition to:

if ($event->isNew && SeoFields::getInstance()?->isInstalled) {

It will avoid that error, however you’ll need to test whether that causes other issues downstream.

@janhenckens
Copy link
Contributor Author

Thanks @brandonkelly! Just so I understand how or why this is happening: the event listener gets triggered even though the plug-in is not enabled yet? But the same behaviour doesn't happen for for example element events? Or is this something specific to the install process?

@brandonkelly
Copy link
Member

@janhenckens Sorry, I realized this is an unintended change in behavior in 4.11 and 5.3, due to this change.

Just released 4.11.2 and 5.3.3 with a fix, so plugins don’t get instantiated until Craft is actually ready to install them.

@janhenckens
Copy link
Contributor Author

Thanks @brandonkelly!

@boboldehampsink
Copy link
Contributor

@brandonkelly these changes seem to break it for me. The schemaVersion is now always 1.0.0 according to Craft

@i-just
Copy link
Contributor

i-just commented Aug 20, 2024

@boboldehampsink, this will happen if the plugin doesn’t specify the $schemaVersion property. Here’s a related issue: #15559. We’ll discuss this internally.

@brandonkelly
Copy link
Member

Craft 4.11.4 and 5.3.5 are out with a fix for this. Thanks again for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants