Simple tricks to use Declarative Schema Magento 2.3

Magento 2.3 has announced a new feature, called Declarative Scheme, which plays an important role in going beyond the installation and updating of the scheme process. Previously, in all versions of Magento, developers used to write a PHP script to install and update setup script data in the database.

The Magento Schema Listener tool converts the Magento 2.3 migration script into a declarative schema.

The declarative scheme approach allows developers to declare the last desired state of the database and automatically adjust it to the system without unnecessarily operating it.
Developers are no longer required to write scripts for each new version. In addition, this approach allows data to be deleted when the module is uninstalled.

We can handle the data definition language (DDL) and the data manipulation language (DML) in the declarative scheme.

How to use Declarative Schema in Magento 2.3?

We are going to learn the entire information which is used to create declarative schema interface and what operation can be done using declarative schema.
Using declarative schema we can perform the following operations:

  • Create a table
  • Drop a table
  • Rename a table
  • Add a column to the current table
  • Drop a column to the current table
  • Change column type
  • Rename a column name or it’s property
  • Add an Index of single column or group of columns
  • Create a foreign key
  • Drop a foreign key
  • Recreate a foreign key

Step 1: Create a table

We hope that you have basic knowledge of creating a new module in Magento. We are going to be creating a custom module in the format below.


We have created a WebtechBuzz_ContactUs on the following directory:

<magento_roor_directory>/ app / code / WebtechBuzz / ContactUs

Create db_schema.xml file after creating the custom module in the following directory:

<magento_roor_directory>app / code / WebtechBuzz / ContactUs / etc / db_schema.xml

We have created a table name called contact_us with five different entities (entity_id, reference_id, name, message, timestamp) in db_schema.xml. We must create db_schema_whitelist.json once we create a database table using declarative schema.

Use the following Magento command to create db_schema_whitelist.json file for our module:

>>bin/magento setup:db-declaration:generate-whitelist –module-name=WebtechBuzz_ContactUs

You can create a db_schema_whitelist.json file manually or by using the above command, it depends on the developer’s choice.

<?xml version = "1.0" ?>
<schema xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="contact_us" resource="default" comment="Create contact us table using declarative schema">
        <column xsi:type="int" name="entity_id" padding="10" identity="true" unsigned="true" nullable="false" comment="Entity Id"/>
        <column xsi:type="smallInt" name="reference_id" padding="5" unsigned="true" nullable="false" comment="Reference Id"/>
        <column xsi:type="varchar" name="name" nullable="false" length="100" comment="Title"/>
        <column xsi:type="text" name="message" nullable="false" length="255" comment="Message"/>
        <column xsi:type="timestamp" name="time" comment="Time"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/&gt;


Top node

The top node represents the schema node, which is located in the schema.xsd file.


The location of the schema.xsd file is: /vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd
Table tag

We can create more than one table in the same db_schema.xml file, each table node creates a new table in the database. A table node can contain the following attributes:

Name : The name of the table

Engine: SQL engine, this value must be InnoDB or memory.

Resource: The database shard on which to install the table. This value must be a default, checkout, or sales.

Comment: Table comments table tag contains three different types of subnodes: Column

IndexColumn TagThe column tag defines inside the table tag, each column tag has its own declaration. A column tag can contain the following attributes:

  • Type: Specifies the column type. Must be one of the following : blob (includes blob, mediumblob, longblob),boolean, date,datetime, decimal,float, int (includes smallint, bigint, tinyint) real (includes decimal, float, double, real),smallint, text (includes text, mediumtext, longtext), timestamp, varbinary, varchar.
  • Name: Name of the column
  • Padding: indicates the size of the integer column.
  • Unsigned: indicates if the column contains positive and negative values or only positive values.
  • Nullable: indicates if the column can be nullable.
  • Comment: indicates the comment of the column.
  • Length: indicates the length of a column constraint tag.
  • Constraints node can contain the following attributes :Type: primary, unique, or foreign
  • Referrence_id : a custom identifier that is used only for relation mapping in the scope of db_schema.xml files

Leave a Reply

Your email address will not be published. Required fields are marked *