I want to insert new field for database table in my custom extension using upgrade schema by following this post,but i got an error saying :

  [Zend_Db_Statement_Exception]                                                
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Category Depth.l  
  ime_eleveniacategory' doesn't exist, query was: DESCRIBE `Category Depth`.`  
  lime_eleveniacategory` 

here's my code:

namespace Test\TestAgain\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
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
        //Your upgrade script
        }
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
          $tableName = $setup->getTable('lime_eleveniacategory'); 
          if ($setup->getConnection()->isTableExists($tableName) == true) {
                $connection = $setup->getConnection();
                $connection->addColumn(
                    $tableName,
                    'category_depth',
                    ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,'nullable' => false, 'afters' => 'category_name'],
                    'Category Depth'
                );
            }
        }
        $setup->endSetup();
    }
}
  • have you created lime_eleveniacategory table? – Rakesh Jesadiya Apr 10 '17 at 9:18
  • @RakeshJesadiya yes the table is there in database – Shell Suite Apr 10 '17 at 9:19
  • please share your full code file – Rakesh Jesadiya Apr 10 '17 at 9:21
  • @RakeshJesadiya check my updated code – Shell Suite Apr 10 '17 at 9:23
  • i have updated answer please check. – Rakesh Jesadiya Apr 10 '17 at 9:28
up vote 22 down vote accepted
namespace Test\TestAgain\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(), "1.0.0", "<")) {
        //Your upgrade script
        }
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
          $installer->getConnection()->addColumn(
                $installer->getTable('lime_eleveniacategory'),
                'category_depth',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'length' => 10,
                    'nullable' => true,
                    'comment' => 'Category Depth'
                ]
            );
        }
        $installer->endSetup();
    }
}

You can get more details here also, Upgrade Database table

  • hello, i have done the same thing but column is not added to table. – Sarfaraj Feb 22 at 7:43

One more thing to do here. Update module.xml version. And upgrade setup, do reindexing and delete cache. It will work.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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