cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a new table using declarative schema in Magento 2.3?

How to create a new table using declarative schema in Magento 2.3?

Declarative Schema aims to simplify the Magento installation and upgrade processes. Before Magento 2.3, extension developers were required to write code (PHP scripts) to change the database schema and It was managed by InstallData, InstallSchema, UpgradeSchema, and UpgradeData. Now, How to create a declarative schema for create a new table in Magento 2.3?

2 REPLIES 2

Re: How to create a new table using declarative schema in Magento 2.3?

Hello @ezekieljose_eze 

 

Declarative schema helps developers with below shared pointers:

  1. Installing and upgrading a code can be handled in a single XML file.
  2. Time-consuming
  3. Performance improvements

You can create Table easily using db_schema.xml file.

 

Follow below shared links for steps:

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html

https://www.dckap.com/blog/declarative-schema-magento-2/

Manish Mittal
https://www.manishmittal.com/

Re: How to create a new table using declarative schema in Magento 2.3?

Hi @ezekieljose_eze 

 

The 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 to be deleted when a module is uninstalled.

 

For creating declarative schema you need to create db_schema.xml in your module etc folder then you need to define your table structure like:

 

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="rh_helloworld" 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="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"/>
        </constraint>
    </table>
</schema>

 

after this please run below command:

1. php bin/magento setup:upgrade

2. php bin/magento setup:db-declaration:generate-whitelist --module-name=YOUR_MODULE_NAME

This will create your table in your database and create a whitelist. for more detail you can go through below docs provided by magento.

 

Configure declarative schema 

Why Declarative Schema 

 

Hope this will help you.

 

If my answer is useful, please Accept as Solution & give Kudos
Shubham Khandelwal