Tuesday, July 21, 2020

Magento 2.3 create the new database table using declarative schema

Hello Magento Folks,

As you know from magento 2.3, use  db_schema.xml  for creating a new table for the database instead of written InstallSchema PHP class. The new database table creation/updating concept in Magento 2.3 is called declarative schema.
Also From Magento 2.3 We don’t need to create InstallSchema.php and UpgradeSchema.php
Now you can create a database table using a new way by creating DB schema XML file lets take a look.
Create a file db_schema.xml
path
app/code/{Vendor}/{Module}/etc/db_schema.xml
We have taken different field types for creating the new database table. You can take the only required onc.
<table>…</table> : “Used for create table in Magento 2.3+”
<column>…</column> : “Used for create different column of the table”
You can set int, text, varchar, timestamp, date, datetime, smallint, float, decimal, double etc.
<constraint>…</constraint> : “Used for set of constraint, Like primary key, foreign key, unique key.”
Upgrade your database to create/update table in Magento 2.3 by using below command
Now you can check in database new table with name “test_table” with all added column will be created.
Hope this code helped you. please feel free to comment in case of any issue.

Happy Coding

No comments: