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.

  • Add your module.xml file code. – Suresh Chikani May 30 '17 at 8:47
  • i have added please check @SHPatel – Kamlesh Jha May 30 '17 at 8:59
  • Any Entry generated in your table? test field is inserted? – Rakesh Jesadiya May 30 '17 at 9:03
  • Check setup_module table, module entry added or not? – Suresh Chikani May 30 '17 at 9:05
  • Entry added @SHPatel – Kamlesh Jha May 30 '17 at 9:16
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 php bin/magento setup:upgrade command.

  • Please let me know if you have issue – Rakesh Jesadiya May 30 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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