How to create schema file to create new database table Magento 2.3?

From Magento 2.3, You want to create a new database table or upgrade an existing table, you can create it with the db_schema.xml file. New database table creation/updating concept in Magento 2.3 is called as declarative schema.

From Magento 2.3 You 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.

Location for db_schema.xml file,
app/code/{Packagename}/{Modulename}/etc/db_schema.xml

You can create your custom table by below XML code,

I have taken many different types of the field for creating the new database table. You can take the only required field to create a database table.

<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.”

You can set primary key usig constraint tag

Where name is your primary key field in the above column tag.

Run Magento 2 Upgrade command to create/update table in Magento 2.3

Now you can check a new table will be generated in a database using db_schema.xml file.

 

Leave a Reply

Your email address will not be published. Required fields are marked *