1

I am trying to add column 'address' on existing upgradeSchema.php, but it not worked.

Here is my code:

<?php

    namespace Testing\Test\Setup;


    use Magento\Framework\DB\Ddl\Table;
    use Magento\Framework\Setup\UpgradeSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;

    class UpgradeSchema implements UpgradeSchemaInterface
    {

        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();

            if (version_compare($context->getVersion(), '1.0.1', '<')) {
                $setup->getConnection()->addColumn(
                    $setup->getTable('testing_test_item'),
                    'description',
                    [
                        'type' => Table::TYPE_TEXT,
                        'nullable' => true,
                        'comment' => 'Item Description'
                    ]
                ); 
                $setup->getConnection()->addColumn(
                    $setup->getTable('testing_test_item'),
                    'address',
                    [
                        'type' => Table::TYPE_TEXT,
                        'nullable' => true,
                        'comment' => 'Item Address'
                    ]
                );
            }
            $setup->endSetup();
        }
    }
    ?>

Module.xml code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Testing_Test" setup_version="1.0.2">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
  • Share your module.xml code as well and Have you run setup:di:compile command? – Chirag Patel Dec 28 '18 at 5:55
  • yes, check the module.xml code (edited question) – Rahul Singh Dec 28 '18 at 6:00
  • Hi @RahulSingh remove (or) comment version condition and execute deploy commands – Nagaraju K Dec 28 '18 at 6:19
  • by removing module schema version entry from setup_module table worked and rest code used the same as in question, thanks – Rahul Singh Dec 28 '18 at 6:23
0

Go to database and find setup_module table and delete your module from database.

Replace your code with below one

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $setup->getConnection()->addColumn(
                $setup->getTable('testing_test_item'),
                'description',
                [
                    'type' => Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Item Description'
                ]
            ); 
            $setup->getConnection()->addColumn(
                $setup->getTable('testing_test_item'),
                'address',
                [
                    'type' => Table::TYPE_TEXT,
                    'nullable' => true,
                    'comment' => 'Item Address'
                ]
            );
        }
        $setup->endSetup();
    }
}

Module.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Testing_Test" setup_version="1.0.2">
    <sequence>
        <module name="Magento_Catalog"/>
        <module name="Magento_Sales"/>
    </sequence>
 </module>
</config>

Run php bin/magento setup:upgrade

I hope it helps!

  • by removing module schema version from setup_module table worked and rest code used the same as in question, thanks – Rahul Singh Dec 28 '18 at 6:22
  • @Rahul Singh Thanks for accepting as answer i have minor changed in if (version_compare($context->getVersion(), '1.0.1') < 0) { condition but any way your problem is solved ;) – Chirag Patel Dec 28 '18 at 6:28
  • +1 for your appreciation :) – Chirag Patel Dec 28 '18 at 6:29
  • @Rahul Singh if you think my answer is good then +1 vote so it is useful to other user. – Chirag Patel Dec 28 '18 at 7:03
0

It might be the version installed is already 1.0.1 try changing it to 1.0.2

if (version_compare($context->getVersion(), '1.0.2', '<')) {
  • by removing module schema version entry from setup_module table worked and rest code used the same as in question, thanks – Rahul Singh Dec 28 '18 at 6:24
0

if you have already installed your module and you run this script it will not work.

check setup_module table in which version installed. if version 1.0.2 then change it to 1.0.3

You have to change module version in module.xml as setup_version="1.0.3" after
run php bin/magento setup:upgrade check the column in table.

Or

You can also use by delete module row from setup_version without changing anything.

it will be reflected.

  • done the same, not worked added module.xml code in question – Rahul Singh Dec 28 '18 at 6:01
  • @RahulSingh have you tried which version in table? – Himanshu Dec 28 '18 at 6:07
  • by removing module schema version entry from setup_module table worked and rest code used the same as in question, thanks – Rahul Singh Dec 28 '18 at 6:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.