Today, we’re going to teach you how to create a new table using declarative schema in the latest Magento 2.3 version.
As we discussed in our previous tutorials, Magento has introduced a new declarative schema feature with its latest Magento 2.3 version. This declarative schema feature basically simplifies installing and updating the schema process.
Also read: How to Drop the Table Using Declarative Schema in Magento 2.3?
In the earlier version of Magento, this process was a bit complex.
For example, in the earlier version first we had to create InstallSchema or InstallData script for creating either a new database table or add new data in an existing database table.
But, with Magento 2.3, we can perform these tasks directly by using the declarative schema db_schema.xml file.
And in this tutorial, we’re going to show you how to use the db_schema.xml file to create a new database table.
Steps to Create a New Table using Declarative Schema in Magento 2.3
Here’s the step-by-step process of creating a new database table using the new declarative schema feature in Magento 2.3.
Step #1
First of all, we will have to create a new db_schema.xml file in the app/code/MD/Helloworld/etc/ directory and copy the following code into the file.
<?xml version="1.0" encoding="UTF-8"?> <!-- /** * Created By : MageDelight Pvt. Ltd. */ --> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="md_helloworld" resource="default" engine="innodb" comment="MD Helloworld"> <column xsi:type="smallint" name="id" padding="7" unsigned="false" nullable="false" identity="true" comment="ID" /> <column xsi:type="varchar" name="author_name" nullable="false" length="20" comment="Name" /> <column xsi:type="varchar" name="email" nullable="false" length="20" comment="Email" /> <column xsi:type="varchar" name="description" nullable="false" length="255" comment="Descrition" /> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="id" /> </constraint> </table> </schema>
Also read: How to Add a New Column to an Existing Table Using db Schema in Magento 2?
Step #2
After that, you need to add your declarative schema in the db_whitelist_schema.json file by executing the following command:
php bin/magento setup:db-declaration:generate-whitelist --module-name=MD_Helloworld
Once you execute the above command, the file db_whitelist_schema.json will be created in the /MD/Helloworld/etc directory.
Step #3
Finally, now we only need to execute the upgrade command given below:
php bin/magento s:up
As you run the upgrade command, a new database table will be created in your database.
Also read: How to Create a Custom Database Table in Magento 2?
Conclusion
And there you have it! This is how easy it is to create a new database table in Magento 2.3 using the declarative schema.
We hope that you found this tutorial helpful. If you’ve any queries, please share them in the comments below.
And if you need our professional assistance, free to contact us at any time.