{"id":2213,"date":"2022-12-05T07:30:43","date_gmt":"2022-12-05T07:30:43","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/update-column-datatype-in-magento-2\/"},"modified":"2025-03-17T07:22:18","modified_gmt":"2025-03-17T07:22:18","slug":"update-column-datatype-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/update-column-datatype-in-magento-2\/","title":{"rendered":"How to Update Column Datatype in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a title=\"Hire Magento Developers\" href=\"https:\/\/meetanshi.com\/hire-magento-developer.html\" target=\"_blank\" rel=\"noopener\">Magento Developers<\/a>&#8230;! Are you looking for ways to <em><strong>update column datatype in Magento 2<\/strong><\/em>? Follow me till the end of this post to find the solution.  \u200d <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Data is the fuel for online businesses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Online stores collect a lot of information on the go, and store them as columns in the <a title=\"How to Create and Upgrade Database in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/create-upgrade-database-in-magento-2\/\" target=\"_blank\" rel=\"noopener\">database<\/a>. Magento 2 stores the information in different data-types for efficient storage and processing. Some of the common data-types are: <code>text<\/code>, <code>integer<\/code>, <code>decimal<\/code>, <code>boolean<\/code>, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But, sometimes the existing data-type may not be suitable for storing the information and you may require changing the column data type in <a title=\"Magento Commerce\" href=\"https:\/\/business.adobe.com\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noopener\">Magento 2<\/a>. There can be multiple instances, such as:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1) The store uses <code>integer<\/code> 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 <code>decimal<\/code> in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2) The store uses <code>varchar<\/code> data-type to store the customer&#8217;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 <code>varchar<\/code> data-type to store information. In order to fix this, you need to update column datatype in Magento 2 to <code>text<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do you relate to the above conditions?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s find out the method to update column datatype in Magento 2.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Change Column DataType in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to update column datatype in Magento 2, we are going to use the <code>changeColumn()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code to the <strong>app\/code\/Vendor\/Extension\/Setup\/InstallSchema.php&nbsp;<\/strong>or&nbsp;<strong>app\/code\/Vendor\/Extension\/Setup\/UpgradeSchema.php<\/strong>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$setup->startSetup(); \n$connection = $setup->getConnection(); \n$connection->changeColumn( \n    $setup->getTable('your_table'), \n    'column_name', \n     [ \n       'type' => \\Magento\\Framework\\DB\\Ddl\\Table::TYPE_TEXT, \n       'length' => 1024, \n       'nullable' => true, \n       'default' => '', \n       'comment' => 'Column Comment' \n     ] \n  ); \n} \n$setup->endSetup();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and yeah&#8230;, you are done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is how easily you can change DataType of the column in Magento 2.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Got any doubts?<\/strong> Feel free to ask. I&#8217;d be glad to help you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If this solution has helped you, rate it with 5 stars. Also, do not forget to <strong>SHARE this post<\/strong> with your developer friends via social media &amp; online communities. \u2728<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento Developers&#8230;! Are you looking for ways to update column datatype in Magento 2? Follow me till the end of this post to find the&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2213","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2213"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2213\/revisions"}],"predecessor-version":[{"id":9467,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2213\/revisions\/9467"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}