How Magento 2 developers Add an Extra Column in Your Existing Magento 2 Table?

  • author-img Nidhi Arora
  • 6 years
How to Add an Extra Column in Your Existing Magento 2

While working with Magento 2, sometimes you need to add an extra column to your already existing Magento 2 table. Today, we are going to provide you with an optimal solution for the same. Our Magento 2 Developers are determined to solve every one of the problems our Magento community face.

But, if you are going through a hard time dealing with the technicalities of Magento 2, consider hiring Best Magento Developers at Envision eCommerce. Our expert developers will make it easy for you to develop robust solutions using the latest version of Magento.

In this blog, we will use the UpgradeSchema.php file for adding extra columns in existing Magento 2 tables and this file should be under the setup folder of the module. So let’s get started.

The below-given example adds an extra column named ‘tok_value’ under the table name ‘sales_order_payment’

Step 1: Create a file called ‘UpgradeSchema.php’ in the setup folder of module, i.e.

(app/code/vendor/modulename/Setup/UpgradeSchema.php)

and make use of following code:

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements  UpgradeSchemaInterface

{

public function upgrade(SchemaSetupInterface $setup,

ModuleContextInterface $context){

$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.1') < 0) {

// Get module table

$tableName = $setup->getTable('sales_order_payment');

// Check if the table already exists

if ($setup->getConnection()->isTableExists($tableName) == true) {

// Declare data

$columns = [

'tok_value' => [

'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,

'nullable' => true,

'comment' => 'Token Value',

],

];

$connection = $setup->getConnection();

foreach ($columns as $name => $definition) {

$connection->addColumn($tableName, $name, $definition);

}

 
}

}

 
$setup->endSetup();

}

}

Step 2: After saving files, you need to run php bin/magento setup:upgrade. Now check your database and you will be able to find a new custom table and new column in ‘sales_order_payment’ table.

Note: If you face any issue, it may be due to the module that you have already installed. As you know, if the module is already installed then setup:upgrade command does not install schema. You will need to look into your setup_module table, delete your module from the table and re-run the php bin/magento setup:upgrade command.

Conclusion:

This “How to” blog on Magento 2 should get you started on adding an extra column to your already existing Magento 2 table. In case, if you feel this “how-to” is like heavy lifting for you, consider availing our Magento 2 development services and we will make it easy and seamless.

We, at Envision Ecommerce, have a team of certified Magento developers who can accomplish anything in Magento 2 for you and your business. Get in touch with us at sales@envisionecommerce.com and we would be more than happy to assist you.

Magento 2 Developers

Download Blog

ENQUIRY

Ready to Get Started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    815 Brazos St STE 500, Austin,
    TX 78701, USA