How to use Declarative Schema in Magento 2.3 and rename an existing table name? - Kishan Savaliya

Welcome to Kishan Savaliya's Magento 2 Blog. In Previous Blog post, we learned how to create a new table using Declarative Schema in Magento 2.3. If you didn't checked that then click on this link and check now, I hope you like that post and that would be useful to you.

In this blog post, I will show you how to rename an existing table name using declarative schema in Magento 2.3 or later versions. If you have old version of Magento then you can check this post to rename an existing table name in Magento 2, there you can rename table name using InstallSchema or UpgradeSchema in your module.


You can rename your existing table name by following below steps..

Go to your Magento 2 Root directory, and navigate inside your custom module where you have created table using db_schema.xml file.

Open db_schema.xml file and modify it's content..

app/code/SK/DeclarativeSchema/etc/db_schema.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * SK DeclarativeSchema Magento 2.3 Rename an existing table name.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
   <table name="sk_declaration_schema_new" onCreate="migrateDataFromAnotherTable(sk_declaration_schema)" resource="default" engine="innodb" comment="SK DeclarativeSchema">
      <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
      <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
      <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" />
      <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
      <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
   </table>
</schema>

Here in <table> tag we can define new table name in name attribute of <table> tag, and we have added one additional attribute onCreate there, we used migrateDataFromAnotherTable() function in that attribute's argument and we can pass old table name there in that function's parameter i.e.

migrateDataFromAnotherTable(sk_declaration_schema)

After that, now we need to generate DB Declatation whitelist by running following command.

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

Whenever you'll run above command it'll generate data in db_whitelist_schema.json file.

app/code/SK/DeclarativeSchema/etc/db_schema_whitelist.json

Generated content of above file is..

{
    "sk_declaration_schema": {
        "column": {
            "id": true,
            "firstname": true,
            "lastname": true,
            "email": true,
            "content": true
        },
        "constraint": {
            "PRIMARY": true
        }
    },
    "sk_declaration_schema_new": {
        "column": {
            "id": true,
            "firstname": true,
            "lastname": true,
            "email": true,
            "content": true
        },
        "constraint": {
            "PRIMARY": true
        }
    }
}

After that, you need to run below command once to rename your table name in your database.

php bin/magento setup:upgrade

Once you run this command, it'll rename your database table name from sk_declaration_schema to sk_declaration_schema_new. You can verify it by running following command in your terminal.

echo "use magento2; SHOW TABLES LIKE '%sk_declaration_schema_new%'" | mysql -uroot -p

If above command will display your renamed table name as output, then it's changed successfully. ;-)


Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.

If you enjoyed this blog post, share it with friends!