php – upgradeSchema is not working in magento 2-ThrowExceptions

Exception or error:

I want to add new column in my existing table. I created an upgradeSchema.php file. And changed version in module.xml file. But after executing upgrade command, nothing happened. Here is my code

<?php 
namespace Bridge\Tradeuser\Setup; 

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

/**
 * {@inheritdoc}
 */
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
    $installer = $setup;

    $installer->startSetup();
            if (version_compare($context->getVersion(), '3.0.1', '<')) {
      $installer->getConnection()
      ->addColumn(
            $installer->getTable('batchcode_entity'),
            'status',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 50,
                'nullable' => false,
                'default' => 'Active',
                'comment' => 'Batchcode status'
            ]
        );

    }
    $installer->endSetup();
}
}  
?>

Then I run the following commands:
bin/magento setup:upgrade

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

bin/magento setup:di:compile

bin/magento cache:flush

How to solve:

Hey Try using this method just change the version number and columns names to match it, I tried right now and it worked

<?php


namespace SimplifiedMagento\Database\Setup;


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


class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
          $setup->startSetup();
          if(version_compare($context->getVersion(),'0.0.2','<'))
          {
                //if version of your project is lower than 0.0.2 then implement this
              $setup->getConnection()->addColumn(
                  $setup->getTable('affiliate_member'),
                  'phonenumber',
                  ['nullable' => false,'type' => Table::TYPE_TEXT,'comment' => 'Phone number column']
              );
          }
          $setup->endSetup();
    }
}

But make sure your module.xml file is having similar version which you wrote in if condition:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SimplifiedMagento_Database" setup_version="0.0.2">
    </module>
</config>

Leave a Reply

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