Hello Magento Lovers,

In this post, I will show you how to create a table using declarative schema.

You need to create db_schema.xml in app/code/Vendor/Module/etc/

    <?xml version="1.0"?>
<!--
/**
 * Copyright © MageFever
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="magefever_table" resource="default" engine="innodb" comment="RH Helloworld">
        <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="varchar" name="first_name" nullable="false" length="50" comment="First Name"/>
        <column xsi:type="varchar" name="last_name" nullable="false" length="50" comment="Last Name"/>
        <column xsi:type="varchar" name="email" nullable="false" length="50" comment="Email"/>

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
    </table>
</schema>
	

 

I have created table "magefever_table" with four fields id (primary key), first_name, last_name, and email.

Next, You have to create db_whitelist_schema.json by running below command:

Once you run this command, you will find db_whitelist_schema.json inside your app/code/Vendor/Module/etc directory.

 

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