How does Magento 2.3 declarative schema work to build new database tables - Magento Source24

 

How does Magento 2.3's declarative schema work to build new database tables?

Hello magento friends, Here we are explane that How does Magento 2.3's declarative schema work to build new database tables?

Starting with Magento 2.3, utilise the db schema.xml file rather than the InstallSchema PHP class to build new database tables. Declarative schema is the name of the new database table creation/updating concept in Magento 2.3.


➤ You no longer need to develop InstallSchema.php and UpgradeSchema.php as of Magento 2.3.

➤  By producing a DB schema XML file, you can now use a new method to build database tables.

➤  The db schema.xml file is located in the following directory: 

app/code/"Packagename"/"Modulename"/etc.

➤  The XML code below can be used to construct your own custom table.


<?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="my_custom_table_name" resource="default" engine="innodb" comment="Custom New Table">
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false"
                identity="true"
                comment="Entity Id"/>
        <column xsi:type="varchar" name="full_name" nullable="true" length="50" comment="Full name"/>
        <column xsi:type="varchar" name="customer_email" nullable="true" length="255" comment="Email"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="true"
                identity="false"
                default="0" comment="Store Id"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false"
                default="CURRENT_TIMESTAMP" comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false"
                default="CURRENT_TIMESTAMP"comment="Updated At"/>
        <column xsi:type="smallint" name="is_active" padding="5" unsigned="true" nullable="false"
                identity="false"
                default="1" comment="Is Active"/>
        <column xsi:type="date" name="user_dob" comment="Date of Birth"/>
        <column xsi:type="text" name="blog_description" nullable="true" comment="Description"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
    </table>
</schema>



➤   I used a variety of different field types to create the new database table. You can make a database table with the lone necessary field.


  1. <table>…</table> : This tag is Used for create table in Magento 2.3+
  2. <column>…</column> : This tag is Used for create different column of the table
  3. You can set int, text, varchar, timestamp, date, datetime, smallint, float, decimal, double etc.
  4. <constraint>…</constraint> :This tag is Used for set of constraint, Like primary key, foreign key, unique key.


➤   You can set primary key usig constraint tag in db_schema: 



<constraint xsi:type="primary" referenceId="PRIMARY">
  <column name="entity_id"/>
</constraint>


where name is the column tag's primary key field.

To create or edit a table in Magento 2.3, use the Magento 2 Upgrade command.

Magento setup:upgrade in PHP

Using the db schema.xml file, you can now verify whether a new table will be created in a database.






Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post