Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento Development 03: Creating Tables

    Before developing frontend part of the module, let’s create some tables. It’s good idea to create the tables beforehand. Because it help us visualise the working of the module, which helps us in developing the module step by step.

    We will be developing a simple blog manager module so we need a table to save the blog data and another table to save the comments. If in future, any other tables are needed or if we have to add/modify any columns then we can easily do that in Magento.

    Declarative Schema

    Magento 2 usage declarative schema to create the tables. To know more about db schema or any topic for that matter please google the topic and you will find multiple blogs which will have explained about that topic. One such great place to learn in detail is Magento Developer Documentation.

    For declarative schema we need to create the db_schema.xml under the etc folder.

    As you can see it is pretty self explanatory, table node/table tag represents the table and the column nodes inside table node represents each columns. We can manage the name, type, length, etc info about a column with the attributes of the tag/node. To create constraint like Primary key, Foreign Key we have to use constraint tag. We can also create indexes with index tag. Here we have created two tables blogmanager_blog, blogmanager_comment.

    After creating the db_schema.xml we need to generate the db_schema_whitelist.json which provides a history of all tables, columns, keys added with declarative schema. You don’t have to worry about this file because it can be generated automatically with command. It’s for backward compatibility and it will be removed in future.

    It will create db_schema_whitelist.json in etc folder.

    Now to actually process the db_schema.xml and create the tables we need to run setup upgrade command.

    After successfully running this command, when you check your database you will find the new tables,

    Now suppose that later we decided to change something, we can just do the changes in the db_schema.xml file, regenerate the db_schema_whitelist.json and run the setup upgrade command.

    Install Schema and Upgrade Schema

    The declarative schema is a new addition to magento. In earlier version of Magento, before 2.3, we had to use InstallSchema.php file to create the tables. And if we later decided to make some changes in the table we had to use UpgradeSchema.php. So first time when we install the module it would run the InstallSchema.php file and for version updates it would run the UpgradeSchema.php.

    Please check out these blogs which explain about using these files. Note that it will not work in our module because we have not mentioned the module version number in the module.xml file.

    Folder Structure till now,

    Next Blog -> Magento Development 04: Model, Resource Model and Collection

    Previous Blog -> Magento Development 02: Route Management and Controllers

    . . .

    Leave a Comment

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

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again