<p>The Craft website has a great article on this, you can simply use migrations following this document: </p>

<p><a href="https://craftcms.com/docs/plugins/migrations" rel="nofollow">https://craftcms.com/docs/plugins/migrations</a></p>

<blockquote>
  <p>If your schema changes over the life of your plugin, you can write a migration to keep existing installations updated with the latest schema. Craft automatically checks for new migrations whenever a plugin’s version number changes.</p>
</blockquote>

<p>In summary, you can create the Record and define all the attributes, then create a migration that would create the new table with those attributes. This would be done in the <code>safeUp()</code> method that utilizes Craft create command:</p>

<p><code>craft()-&gt;db-&gt;createCommand()-&gt;createTable('new_table_record_name', $attributesArray, null, true);</code></p>

<p>When the plugin is updated, it will run the migrations and create the table.</p>

CC BY-SA 3.0
2016-07-15T18:55:58.877Z
I took a similar approach, but instead of using ```craft()->db->createCommand()``` I created an instance of the record model using the "install" scenario and then followed the lead from what plugins do when they install. So it was like

```$record = new PluginName_DataRecord('install'); 
$record->createTable();
$record->addForeignKeys();
```

Is there any advantage/disadvantage to this method?
John O
CC BY-SA 3.0
2016-05-20T17:30:42.050Z
2016-05-20T17:30:42.050Z
Jason McCallister
<p>I'm working on a plugin, and would like to add a new Record, I already have some records, and would like to keep the data.</p>

<p>Is there a way to activate a new record type, without re-installing the plugin, thus loosing the other records data?</p>

<plugin-development><records>
Add Record without deleting data
# Add Record without deleting data

(Asked by: username_0 on 2016-05-20)

I'm working on a plugin, and would like to add a new Record, I already have some records, and would like to keep the data.

Is there a way to activate a new record type, without re-installing the plugin, thus loosing the other records data?

**Tags:** plugin-development, records


---

## 1 answers

### Answer by username_1 on 2016-05-20. Score: 2

The Craft website has a great article on this, you can simply use migrations following this document:

<https://craftcms.com/docs/plugins/migrations>

> If your schema changes over the life of your plugin, you can write a migration to keep existing installations updated with the latest schema. Craft automatically checks for new migrations whenever a plugin’s version number changes.

In summary, you can create the Record and define all the attributes, then create a migration that would create the new table with those attributes. This would be done in the `safeUp()` method that utilizes Craft create command:

`craft()->db->createCommand()->createTable('new_table_record_name', $attributesArray, null, true);`

When the plugin is updated, it will run the migrations and create the table.

> **Comments:**
> **username_2**: I took a similar approach, but instead of using ```craft()->db->createCommand()``` I created an instance of the record model using the "install" scenario and then followed the lead from what plugins do when they install. So it was like  ```$record = new PluginName_DataRecord('install');  $record->createTable(); $record->addForeignKeys(); ```  Is there any advantage/disadvantage to this method?  

---

