How to Rename a database tablename using db_schema in Magento 2 - Magento Source24

 

How to Rename a database tablename using db_schema in Magento 2

Hello Magento friends todays im share with you to How to Rename a database tablename using db_schema in Magento 2

Using the db schema.xml file, you can add new tables in Magento 2. If you want to change a table's name in magento2 after it has been created, you can do so by utilising declarative schema.


<?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="my_new_custom_table" onCreate="migrate_Data_From_Another_Table">
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true"
                nullable="false" identity="true"
                comment="Entity Id"/>
        <column xsi:type="varchar" name="full_name" nullable="true" length="50"
                comment="Full name"/>
        <column xsi:type="varchar" name="customer_email" nullable="true" length="255"
                comment="Email"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true"
                nullable="true" identity="false"
                default="0" comment="Store Id"/>
    </table>
</schema>


➤ To create the db schema whitelist.json file, you must now run the generate-whitelist command.


command : php bin/magento setup:db-declaration:generate-whitelist –module-name=Vendorname_Packagename


➤ Where Vendor_Name_Package_Name is your Vendor and Package_Name.

➤ After that , you can change the table name using below tag in the XML file  in magento2:



<table name="rename_tablename" onCreate="Migrate_Data_From_Another_Table"></table>


➤ where rename_tablename is your new table name and mycustom_table is your old table name in the above tag.

The new db_schema.xml file will be,

➤ After Add Code in db_schema.xml file, you need to run this bellow command : 


php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush












Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post