13

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();
    }
}
5
  • have you created lime_eleveniacategory table? Apr 10, 2017 at 9:18
  • @RakeshJesadiya yes the table is there in database Apr 10, 2017 at 9:19
  • please share your full code file Apr 10, 2017 at 9:21
  • @RakeshJesadiya check my updated code Apr 10, 2017 at 9:23
  • i have updated answer please check. Apr 10, 2017 at 9:28

4 Answers 4

42
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

6
  • hello, i have done the same thing but column is not added to table. Feb 22, 2018 at 7:43
  • How to create new table using upgradeschema. i have module already Jan 24, 2019 at 15:30
  • hello, @Rakesh, how to add the new columns in between the exiting columns? May 9, 2019 at 12:09
  • @Rakesh How can I pass version there? I'm saying about 1.0.1
    – Magecode
    Aug 5, 2019 at 10:12
  • In my module.xml setup_version="2.0.0"
    – Magecode
    Aug 5, 2019 at 10:14
3

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

3

To Add multiples columns

    if (version_compare($context->getVersion(), '0.1.1', '<')) {
        $installer->getConnection()->addColumn(
            $installer->getTable('reply_newsletter_subscriber'),
            'field_1',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment' => 'Field_1'
            ]

        );
        $installer->getConnection()->addColumn(
            $installer->getTable('reply_newsletter_subscriber'),
            'field_2',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment' => 'Field_2'
            ]
        );
    }
    $installer->endSetup();
}
0

I tried this

    <?php


namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Quote\Setup\QuoteSetup;
use Magento\Sales\Setup\SalesSetup;

class InstallData implements InstallDataInterface
{
    protected $quoteSetupFactory;
    protected $salesSetupFactory;

    public function __construct(
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
         $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

        $setup->startSetup();

        $setup->startSetup();
        $quoteInstaller->addAttribute('quote', 'custom_column', ['type' => Table::TYPE_TEXT]);
        $salesInstaller->addAttribute('order', 'custom_column', ['type' => Table::TYPE_TEXT]);
        $setup->endSetup();


        }
}

OR

<?php


namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Quote\Setup\QuoteSetup;
use Magento\Sales\Setup\SalesSetup;

class UpgradeSchema implements UpgradeSchemaInterface
{


    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {


         $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();
        $connection->addColumn($installer->getTable('quote'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);
        $connection->addColumn($installer->getTable('sales_order'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);

        $installer->endSetup();


        }
}

Note: If you face any issue, it may be due to the module that you have already installed. As you know, if the module is already installed then setup:upgrade command does not install schema. You will need to look into your setup_module table, delete your module from the table and re-run the php bin/magento setup:upgrade command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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