0

I made a Magento 2 module for adding an extra column product_custom_type into an existing table sales_order_item

I run the upgrade script with this command

php bin/magento setup:upgrade

the last output i got was "Upgrading schema...", then command quits.

This is my UpgradeSchema.php

namespace Scandesigns\ProductType\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '2.0.1') < 0) {
            $setup->startSetup();
            $setup->getConnection()->addColumn(
                $setup->getTable('sales_order_item'),
               'product_custom_type',
               [
                   'type' => \Magento\Framework\DB\Ddl\Table::TYPE_STRING,
                   'length' => '256',
                   'nullable' => false,
                   'default' => '',
                   'comment' => 'product custom type'
               ]
           );
           $setup->endSetup();
        }
    }
}

Did I do anything wrong in the code?

1
  • what's the module version declared in etc/module.xml and what's the version from the setup_module table for your module?
    – Marius
    Feb 22, 2017 at 14:30

1 Answer 1

3

Try below code and make sure setup version in module.xml file is greater then 2.0.1

<?php 

namespace Scandesigns\ProductType\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();

        if (version_compare($context->getVersion(), '2.0.1', '<')) {
            $connection->addColumn(
            $installer->getTable('sales_order_item'),
               'product_custom_type',
               [
                   'type' => Table::TYPE_TEXT,
                   'length' => 256,
                   'nullable' => false,
                   'default' => '',
                   'comment' => 'product custom type'
               ]
           );
        }
         $installer->endSetup();
    }
}
2
  • great. Your code worked for me. now the filed is added to the table. I can see you have changed the type to TYPE_TEXT. anything else I did wrong? Thanks so much for the help. Feb 22, 2017 at 15:25
  • 1
    you have to add "use Magento\Framework\DB\Ddl\Table;" to get table info. In addition, setup start and end should be outside of "if" condition. That's it. Feb 22, 2017 at 15:36

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.