How To Create Schema File To Create New Database Table In Magento 2.3
A database table to save the Magento 2 store data can be created using two methods. One of the methods to create a new database table in Magento 2.3 is with the db_schema.xml file.
The post shows how to create schema file to create new database table in Magento 2.3.
Apart from the below code, you can refer DB schema structure for Magento 2.
Method to Create Schema File To Create New Database Table in Magento 2.3:
Create db_schema.xml file at app/code/Vendor/Extension/etc/ directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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="mt_distance_shipping_warehouse" resource="default" engine="innodb" comment="Distance Shipping Warehouse Table"> <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/> <column xsi:type="varchar" name="longitude" comment="Longitude" nullable="false"/> <column xsi:type="varchar" name="latitude" comment="Latitude" nullable="false"/> <column xsi:type="varchar" name="street" comment="Street" nullable="false" /> <column xsi:type="varchar" name="city" comment="City" nullable="false" /> <column xsi:type="varchar" name="state" comment="State" nullable="false" /> <column xsi:type="varchar" name="country" comment="Country" nullable="false" /> <column xsi:type="varchar" name="zipcode" comment="Zipcode" nullable="false" /> <column xsi:type="boolean" name="status" comment="Status" nullable="false" /> <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"/> <constraint xsi:type="primary" referenceId="PRIMARY"> <column name="id"/> </constraint> </table> </schema> |
That’s it.
The next step is to create a new database table in Magento 2.3.
The doubts in the implementation of creating a schema file for creating a new database table in your Magento 2.3 store can be mentioned in the Comments section below. I’d be happy to help.
Do share the solution with fellow developers of Magento community via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Everything About search_query Table In Magento 2
A Blueprint to Add Magento 2 Terms And Conditions Checkbox
Next