Table of Contents
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.
You can use the below code for the same. For example, adding an order delivery date column to the order table in the database.
To Add A Column To Existing Database Table In Magento 2
There are two ways of doing this –
- Using UpgradeSchema (Used in Magento 2.3 earliar version)
- Using DB Schema
Method 1: Using UpgradeSchema:
This method is used earliar Magento 2.3.Β In this method, 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 βdelivery_dateβ under the table name βsales_orderβ.
Note:
Increase the setup_version in the app/code/Vendor/Module/etc/module.xml file and mention the same in below file.
Let’s create UpgradeSchema.php in the path app/code/Vendor/Module/Setup
<?php namespace Vendor\Module\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\DB\Ddl\Table; class UpgradeSchema implements UpgradeSchemaInterface { public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($contextβ>getVersion(), β1.0.1β) < 0) { $connection = $setupβ>getConnection(); $connectionβ>addColumn( $setupβ>getTable(βsales_orderβ), βdelivery_dateβ, [ βtypeβ => Table::TYPE_TEXT, βlengthβ => 255, βnullableβ => true, βdefaultβ => β, βcommentβ => βAdd New Filedβ ] ); } } }
After saving files, you need to run
php bin/magento s:up
Now check your database and you will be able to find a new column ‘delivery_date’ in βsales_orderβ table.
Method 2:Β USING DB SCHEMA IN MAGENTO 2
In the previous version of Magento, this was done by using aΒ UpgradeSchema.phpΒ file.
But, in the Magento 2.3 version or later version, we can add a new column to an existing table using the db_schema.xml file.
Hereβs an easy and step-by-step process for adding a new column to an existing table using db_schema.xml file in Magento 2.
Let’s say, we have table abc with columns id, title, email, description. And now we want to add column short_desctiption in this table ‘abc’.
Step 1: Create a db_schema.xmlΒ file
The first step of the process is to create aΒ db_schema.xml file in the app/code/Vendor/Module/etc/Β directory
app/code/Vendor/Module/etc/db_schema.xml
and Content for this file is..
<?xml version="1.0"?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> Β Β Β <table name="abc" resource="default" engine="innodb" comment="ABC is the table name"> Β Β Β Β Β Β <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID" /> Β Β Β Β Β Β <column xsi:type="varchar" name="title" nullable="false" length="25" comment="Title" /> Β Β Β Β Β Β <column xsi:type="varchar" name="email" nullable="false" length="25" comment="Email" /> Β Β Β Β Β Β <column xsi:type="varchar" name="description" nullable="false" length="255" comment="Descrition" /> Β Β Β Β Β Β <column xsi:type="varchar" name="short_description" nullable="false" length="255" comment="Short Description" /> Β Β Β Β Β Β <constraint xsi:type="primary" referenceId="PRIMARY"> Β Β Β Β Β Β Β Β Β <column name="id" /> Β Β Β Β Β Β </constraint> Β Β Β </table> </schema>
As you can see, weβve added a new column short_description in the abc table.
Step 2: Add the schema toΒ db_whitelist_schema.jsonΒ file
Now, before executing the upgrade command, youβll have to add the schema toΒ db_whitelist_schema.json file. This provides a history of all tables, columns, and keys added with declarative schema. It is required to allow drop operations. It can be generated manually or created automatically with the following command:
php bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Module
Once you execute the above command, theΒ db_whitelist_schema.jsonΒ file will be created in theΒ /Vendor/Module/etcΒ directory.
Step 3: Run setup upgrade command
Lastly, you need to run the setup upgrade command given below after theΒ db_schema_whitelist_jsonΒ file has been successfully generated.
php bin/magento s:up && php bin/magento se:s:d -f
Now, check the output and youβll see that a new column will be created in your database table.
Conclusion
So, this is how easy it is to add a new column to an existing database table using db schema in Magento 2.
We hope that you found this tutorial helpful. If youβve any queries, please share them in the comments below.
For the db_schema method is it required to write column of existing table (id, title, email, description) along with new column (short_description).
Yes
now..how to add datas into those new columns…
hope you will help me…
Follow the step of adding data from this video – https://youtu.be/3zjYfxmZUpM