How to use Declarative Schema in Magento 2.3 and Create new table? - Kishan Savaliya

Welcome to Kishan Savaliya's Magento 2 Blog. In this blog post,we will learn how to use declarative schema in Magento 2.3 and above versions and how to create new table using it.

Before Magento 2.3, we were able to create new tables using InstallSchema, InstallData, UpgradeSchema and UpgradeData. One can use that feature in latest version as well, but in newer version Magento introduced new way that is declarative schema, using this we can do some operations, like Create new table, Add new column, Remove any existing columns, Rename table name etc..

In Magento 2.3 and later versions, we can create db_schema.xml file inside etc/ directory and we can perform all above actions using it. You can check more details here in official documentation of Magento.

So let's start about how to use declarative schema in Magento 2.3 and learn to create a new table using it.


Go to you Magento 2 root directory and navigate inside your custom module directory. If you don't have any custom module, then you can create one. If you don't know how to create new module in Magento 2 then you can follow this tutorial and you can create your simple new custom module easily. How to create new custom module in Magento 2?

Navigate inside etc directory in your module and create db_schema.xml file inside it.

Create db_schema.xml file here

app/code/SK/DeclarativeSchema/etc/db_schema.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * SK DeclarativeSchema Magento 2.3 Create new table.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
   <table name="sk_declaration_schema" resource="default" engine="innodb" comment="SK DeclarativeSchema">
      <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
      <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
      <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" />
      <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
      <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
   </table>
</schema>

Generate DB Declaration whitelist by running following command..

php bin/magento setup:db-declaration:generate-whitelist --module-name=SK_DeclarativeSchema

Whenever you'll run above command it'll generate db_schema_whitelist.json file at same location here.

app/code/SK/DeclarativeSchema/etc/db_schema_whitelist.json

Content generated in that file is..

{
    "sk_declaration_schema": {
        "column": {
            "id": true,
            "firstname": true,
            "lastname": true,
            "email": true,
            "content": true
        },
        "constraint": {
            "PRIMARY": true
        }
    }
}

Now you can run below command once to generate this new table in your database.

php bin/magento setup:upgrade

That's it. New table will be created after running above command in your database, so you can verify it is generated properly or not, using below command in your CLI. If table is created successfully in your database then it will return your table name.

echo "use magento2; SHOW TABLES LIKE '%sk_declaration_schema%'" | mysql -uroot -p

Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.

If you enjoyed this blog post, share it with friends!