1

I've got a plugin that works on Craft 4.5. It's been installed a long time, upgraded from 2.x to 3.x.

Now trying to use it on a new install of Craft 4.5.10, installing it does not create any tables. Below is one of the records. So, three questions:

  1. In the CP Plugins page, should clicking install or uninstall create tables or remove them, respectively?
  2. Is it necessary to have a migration in order for the tables to be created?
  3. Any suggestions where to look for what's the problem?

Thank for any hints!

<?php
namespace kr37\drycalendar\records;

use Craft;
use craft\db\ActiveRecord;

class Drycalendar extends ActiveRecord
{
    public static function getTableName() :string
    {   
        return '{{%drycalendar}}';
    }   

    protected function defineAttributes() {
        return array(
            // Craft automatically creates 'id' as an autoincrement
            'event_id'   => AttributeType::Number,
            'dateYmd'    => AttributeType::String,
            'timestr'    => AttributeType::String,
            'alt_text'   => AttributeType::String,
            'css_class'  => AttributeType::String,
            'userJson'   => AttributeType::String,
        );
    }

    public function defineIndexes() {
        return array(
            array('columns' => array('event_id', 'dateYmd'), 'key' => true),
        );
    }

}
0

1 Answer 1

2

You need to have a migration in your plugin to install the necessary tables and perform other plugin installation/uninstallation actions.

Just because you have a Records/Model class doesn't mean your plugin will automatically detect it and create it. You need to explicitly write this during the installation migration(safeUp() on the instalaltion & safeDown() on the uninstallation in the *pluginName*/Install.php file)

  1. So, if your plugin has migrations, then clicking the Install/Uninstall will trigger these migrations, for example, create/drop table(s).

  2. Yes!

  3. Make sure your plugin actually has some kind of install/uninstall action.

3
  • Super helpful! Thank you.
    – kr37
    Commented Nov 12, 2023 at 13:26
  • 1
    I think a reason for my confusion is that this must have changed after Craft 2.0. It seems it used to be like this: If you add entirely new records to your plugin, the tables will automatically get created for people just installing your plugin. From link
    – kr37
    Commented Nov 12, 2023 at 19:28
  • Yep, it seems so. I didn't know that either.
    – RomanAvr
    Commented Nov 12, 2023 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.