2

I have already created a table with name tutorial_simplenews.I want to add new column to that table.my code is below.

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Tutorial\SimpleNews\Setup;

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

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.0') < 0) {
            $connection = $setup->getConnection();

            $connection->addColumn(
              $setup->getTable('tutorial_simplenews'),
            'test',
            ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INT,
            'length' => '11',
           'nullable' => false,
         'default' => '0',
           'comment' => 'Jute Is Allow Status']);





        $setup->endSetup();
    }
}

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../htdocs/lib/internal/Magento/Framework/Module/etc/module.xsd">
     <module name="Tutorial_SimpleNews" setup_version="1.0.0" schema_version="1.0.0">

    </module>
</config>

Please can you any one help me, where am i doing wrong in above code.Any references or suggestions are highly appreciated.

8
  • Add your module.xml file code. May 30, 2017 at 8:47
  • i have added please check @SHPatel May 30, 2017 at 8:59
  • Any Entry generated in your table? test field is inserted? May 30, 2017 at 9:03
  • Check setup_module table, module entry added or not? May 30, 2017 at 9:05
  • Entry added @SHPatel May 30, 2017 at 9:16

1 Answer 1

0

Set 1.0.1 setup version in your module.xml file,

<?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="Tutorial_SimpleNews" setup_version="1.0.1" schema_version="1.0.1">      
    </module>
</config>

Now your upgradeschema.php file,

 <?php
    namespace Tutorial\SimpleNews\Setup;
    
    use Magento\Framework\Setup\UpgradeSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    
    /**
     * @codeCoverageIgnore
     */
    class UpgradeSchema implements UpgradeSchemaInterface
    {
        /**
         * {@inheritdoc}
         */
        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {        
            $installer = $setup;
            $installer->startSetup();
            if (version_compare($context->getVersion(), '1.0.1') < 0) {
                $installer->getConnection()->addColumn(
                    $installer->getTable('tutorial_simplenews'),
                    'test',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INT,
                        'nullable' => true,
                        'length' => '11',                    
                        'default' => '0',
                        'comment' => 'Jute Is Allow Status'
                    ]
                );
            }
            $installer->endSetup();
        }
    }

Run below commands php bin/magento cache:clean and php bin/magento setup:upgrade

1
  • Please let me know if you have issue May 30, 2017 at 10:12

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.