Magento 2 Implement Declarative Schema in Custom Module.

In this post, I’ll explain to you how to Implement Declarative Schema in the Custom Module.

To implement declarative schema in a custom module you need to create db_schema.xml file inside /Vendor/Modulename/etc and write the following code.

<?xml version="1.0"?>
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
<schema xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="vendor_tablename" resource="default" engine="innodb" comment="Vendor Modulename">
        <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="author_name" nullable="false" length="25" comment="Name"/>
        <column xsi:type="varchar" name="email" nullable="false" length="25" comment="Email"/>
        <column xsi:type="varchar" name="description" nullable="false" length="255" comment="Descrition"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>

Use for create and set table names.

<table> .. </table>

Use for create and set columns of the table.

<column> .. </column>

Use for set constraint as like primary key, foreign key, unique key, etc.

<constraint> .. </constraint>

Before running the upgrade command you need to add your schema to db_whitelist_schema.json file by running the following command:

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

Now, there are db_whitelist_schema.json file will be created in /Vendor/Modulename/etc folder. Now, run

php bin/magento s:up

Now the table will be created inside the database.

In case if you want to rename a column, you will need to do few code changes in below line your db_schema.xml at appropriate column.

<column xsi:type="varchar" name="customer_email" onCreate="migrateDataFrom(email)" on_update="false" nullable="false" default="" comment="Customer Email"/>

Here, name="customer_email" is a new column name and onCreate="migrateDataFrom(email)" email is old column name.

Also, if you want to drop table, then you can either remove entire table node from an XML file or you can set a disabled attribute to true as like below line in your db_schema.xml

<table name="Vendor_Modulename" resource="default" engine="innodb" comment="Vendor Modulename" disabled="true">

That’s it.

I hope this post helped you to find what you were looking for. Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this note with your team.

Review other articles maybe it'll help you too.