Uncategorized Declarative Schema, Declarative Schema feature in Magento 2.3, magento2.3

Declarative Schema in Magento 2.3

Magento2.3 has introduced a new feature called Declarative Schema which aims to simplify the Magento installation and upgrade processes.

The new declarative schema approach allows developers to declare the final desired state of the database and has the system adjust to it automatically, without performing redundant operations. Developers are no longer forced to write scripts for each new version. In addition, this approach allows data be deleted when a module is uninstalled.

In the Module/etc/ folder create db_schema.xml file where you will define your tables, columns, or say everything you need.

<?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="utm_tracking">
        <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" comment="Id"/>
        <column xsi:type="varchar" name="source" length="255" unsigned="true" nullable="false" comment="UTM Source"/>
        <column xsi:type="varchar" name="medium" nullable="true" length="255" comment="UTM Medium"/>
		<column xsi:type="varchar" name="campaign" nullable="true" length="255" comment="UTM Campaign"/>
        <column xsi:type="timestamp" name="time" padding="10" comment="Time of event"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
    </table>
</schema>

Running setup:upgrade will create a table name utm_track

    The table node can contain three different types of subnodes:
  • column
  • constraint
  • index

For more details on table node, read

We must create db_schema_whitelist.json once we create a database table using declarative schema.

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

You can create a db_schema_whitelist.json file manually or by using the above command

Leave a Reply

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