skip to Main Content

I want to create my custom table in my custom module. how to create it in Magento 2.3. Is there any other way of install schema? I know for magento 1.9.

2

Answers


  1. Chosen as BEST ANSWER

    From magento 2.3 their is new way of installing schema. You have to create new xml file in etc/db_schema.xml for installing schema.

    for more detail please check the file at vendor/magento/module-cms/etc/db_schema.xml


  2. First of all, create db_schema.xml file inside /Vendor/Module/etc and write the following code :

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
        <table name="table_name" resource="default" engine="innodb" comment="comment_here">
            <column xsi:type="smallint" name="column_name" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
            <column xsi:type="varchar" name="column_name" nullable="false" length="25" comment="Name"/>
            <column xsi:type="varchar" name="column_name" nullable="false" length="25" comment="Email"/>
            <column xsi:type="varchar" name="column_name" nullable="false" length="255" comment="Descrition"/>
            <constraint xsi:type="primary" referenceId="PRIMARY">
                <column name="id"/>
            </constraint>
        </table>
    </schema>
    
    • <table> .. </table> = “Use for create and set table name”
    • <column> .. </column> = “Use for create and set column of the table”
    • <constraint> .. </constraint> = “Use for set constraint as like
      primary key, foreign key, unique key etc.”

    Before running the upgrade command you need to add your schema to db_whitelist_schema.json file by running the following command :

    php bin/magento setup:db-declaration:generate-whitelist --module-name=vendor_module
    

    Now, there are db_whitelist_schema.json file will be create in /vendor/module/etc folder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search