Answers.Body: <p><strong>You need to have</strong> <a href="https://craftcms.com/docs/4.x/extend/migrations.html#plugin-install-migrations" rel="nofollow noreferrer">a migration in your plugin to install the necessary tables</a> and perform other plugin installation/uninstallation actions.</p>
  <p>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(<code>safeUp()</code> on the instalaltion &amp; <code>safeDown()</code> on the uninstallation in the <code>*pluginName*/Install.php</code> file)</p>
  <ol>
  <li><p>So, if your plugin has migrations, then clicking the Install/Uninstall will trigger these migrations, for example, create/drop table(s).</p>
  </li>
  <li><p>Yes!</p>
  </li>
  <li><p>Make sure your plugin actually has some kind of install/uninstall action.</p>
  </li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-12T13:26:07.767Z
Answers.Text: Super helpful! Thank you.
Answers.UserDisplayName: kr37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-12T19:28:13.173Z
Answers.Text: 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](https://craftcms.com/docs/2.x/plugins/migrations.html)
Answers.UserDisplayName: kr37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-12T20:44:44.390Z
Answers.Text: Yep, it seems so. I didn't know that either.
Answers.UserDisplayName: RomanAvr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-12T08:32:18.877Z
Answers.LastActivityDate: 2023-11-12T08:32:18.877Z
Answers.UserDisplayName: RomanAvr
Body: <p>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.</p>
  <p>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:</p>
  <ol>
  <li>In the CP Plugins page, should clicking <em>install</em> or <em>uninstall</em> create tables or remove them, respectively?</li>
  <li>Is it necessary to have a migration in order for the tables to be created?</li>
  <li>Any suggestions where to look for what's the problem?</li>
  </ol>
  <p>Thank for any hints!</p>
  <pre><code>&lt;?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'   =&gt; AttributeType::Number,
              'dateYmd'    =&gt; AttributeType::String,
              'timestr'    =&gt; AttributeType::String,
              'alt_text'   =&gt; AttributeType::String,
              'css_class'  =&gt; AttributeType::String,
              'userJson'   =&gt; AttributeType::String,
          );
      }
  
      public function defineIndexes() {
          return array(
              array('columns' =&gt; array('event_id', 'dateYmd'), 'key' =&gt; true),
          );
      }
  
  }
  </code></pre>
  
Tags: <plugin-development><migration><records>
Title: Tables not created when plugin installed
ThreadText: # Tables not created when plugin installed
  
  (Asked by: username_0 on 2023-11-09)
  
  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 username_0\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),
          );
      }
  
  }
  
  ```
  
  **Tags:** plugin-development, migration, records
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-11-12. Score: 2
  
  **You need to have** [a migration in your plugin to install the necessary tables](https://craftcms.com/docs/4.x/extend/migrations.html#plugin-install-migrations) 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.
  
  > **Comments:**
  > **username_0**: Super helpful! Thank you.  
  > **username_0**: 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](https://craftcms.com/docs/2.x/plugins/migrations.html)  
  > **username_1**: Yep, it seems so. I didn't know that either.  
  
  ---
  
  