How to Update Column Datatype in Magento 2
Magento Developers…! Are you looking for ways to update column datatype in Magento 2? Follow me till the end of this post to find the solution. π±βπ
Data is the fuel for online businesses.
Online stores collect a lot of information on the go, and store them as columns in the database. Magento 2 stores the information in different data-types for efficient storage and processing. Some of the common data-types are: text
, integer
, decimal
, boolean
, etc.
But, sometimes the existing data-type may not be suitable for storing the information and you may require changing the column data type in Magento 2. There can be multiple instances, such as:
1) The store uses integer
data type for storing the product prices (For e.g. $100). However, the client wants to show the product price in decimals in the front end (For e.g. $99.99). In such cases, you need to change the data type of the product price column from integer to decimal
in Magento 2.
2) The store uses varchar
data-type to store the customer’s address, and you noticed many of the addresses in the database are incomplete. A likely cause of this condition may be the limitation of the varchar
data-type to store information. In order to fix this, you need to update column datatype in Magento 2 to text
.
Do you relate to the above conditions?
Let’s find out the method to update column datatype in Magento 2. π«
Method to Change Column DataType in Magento 2
In order to update column datatype in Magento 2, we are going to use the changeColumn()
function.
Add the following code to the app/code/Vendor/Extension/Setup/InstallSchema.phpΒ orΒ app/code/Vendor/Extension/Setup/UpgradeSchema.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$setup->startSetup(); $connection = $setup->getConnection(); $connection->changeColumn( $setup->getTable('your_table'), 'column_name', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'length' => 1024, 'nullable' => true, 'default' => '', 'comment' => 'Column Comment' ] ); } $setup->endSetup(); |
and yeah…, you are done!
This is how easily you can change DataType of the column in Magento 2. π
Got any doubts? Feel free to ask. I’d be glad to help you.
If this solution has helped you, rate it with 5 stars. Also, do not forget to SHARE this post with your developer friends via social media & online communities. β¨
Thanks for reading. π
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Change Shopify Store Name [2024]
5 Common Shopify SEO Issues and How to Fix Them
Next