# Run SQL in Migration to manupulate data?

**URL:** <https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702>\
**Category:** Need Help\
**Created:** [September 10, 2025, 4:05am UTC](https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702 "2025-09-10T04:05:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![general2000vn](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@general2000vn](https://discourse.cakephp.org/u/general2000vn)\
**Post date:** [September 10, 2025, 4:05am UTC](https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702/1 "2025-09-10T04:05:28Z")

</div>

Hello everyone.

I have a table which has 1 column name **type (varchar 25)**, now I am writing Migration, adding another column named type\_id (int).

What I also want is migration automatically add value for existing data based on their current value of type column. (e.g: chair = 1, table = 2, bed = 3 …..).

Is it posible to do that in Migration? Would you please write a block of code doing that please?

Thank you very much

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 10, 2025, 1:42pm UTC](https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702/2 "2025-09-10T13:42:07Z")

</div>

You can call `$this->execute("custom SQL query here");` in your migration to update the data. Probably an `update` with a `case` would be appropriate? How would you do this update if you did it manually in SQL? Just do that with an `execute` in your migration.

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [September 11, 2025, 6:10am UTC](https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702/3 "2025-09-11T06:10:22Z")

</div>

Yes, it is possible. and everything is described in the documentation.

> **[Writing Migrations - 0.16](https://book.cakephp.org/phinx/0/en/migrations.html#inserting-data)**

> **[Saving Data - 5.x](https://book.cakephp.org/5/en/orm/saving-data.html)**

---

<div class="post-metadata">

**Author:** ![general2000vn](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@general2000vn](https://discourse.cakephp.org/u/general2000vn)\
**Post date:** [September 16, 2025, 1:27am UTC](https://discourse.cakephp.org/t/run-sql-in-migration-to-manupulate-data/12702/4 "2025-09-16T01:27:56Z")

</div>

Thank you Zuluru and Salines a lot
