How to rename/change column name for database table using Magento 2?

In Magento 2 Sometimes we have added a column/field in the table for our custom requirement and in future if we need to rename or change that column name, we can rename it using UpgradeSchema.php file.

I have added one field name guestname in sales_order table and I want to rename that field to fullname. You can rename field using changeColumn() in UpgradeSchema.php file.
Using below code snippet you can rename already existed column field from a table.

Upgrade the setup_version number of module from module.xml file. An older version number is 1.0.1 so I have added new version 1.0.2 in module.xml file

Path: Magento22/app/code/Rbj/Training/etc/module.xml

Create UpgradeSchema.php file under Setup folder in your module.Path: Magento22/app/code/Rbj/Training/Setup/UpgradeSchema.php 

Using the above code snippet you can change column name from guestname to fullname with 255 character type text

Run command from root directory of your project,php bin/magento setup:upgrade

If you want to drop field name from a table, Remove an extra field from a table

If you want to add the field in table  Add New Field in Database Table Magento 2

Leave a Reply

Your email address will not be published. Required fields are marked *