11

I'm trying to add new column to existing table in magento2

<?php

namespace Vendor\Module\Setup;

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

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $eavTable = $installer->getTable('eav_attribute');

        $columns = [
            'my_column' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                'length' => '1',
                'nullable' => false,
                'comment' => 'Description of my column',
            ],
        ];

        $connection = $installer->getConnection();
        foreach ($columns as $name => $definition) {
            $connection->addColumn($eavTable, $name, $definition);
        }

        $installer->endSetup();
    }
}

php bin/magento setup:upgrade

Nothing happens

Upd 1.

If I clearly understand the goal, InstallSchema executes only when there no any values in setup table. If your module is already installed in system - you need make any changes in UpgradeSchema. That because my file didn't execute. When I renamed it to upgrade and make necessary changes - everything started working properly

3 Answers 3

7

First, I'm assuming when you say that nothing happens, you mean that the setup script runs as usual, but no errors are output and no changes were made to your database. If this isn't a correct assumption, please let me know!

When I do an upgrade to the schema, I don't do a getTable(), I think that that is superfluous.

I tested the above script with that change, and it worked, so the two troubleshooting steps I would take are to:

  1. Ensure that you have incremented the setup_version in your module.xml (or your script won't run at all)
  2. Add some obvious error in your upgrade script to see if it is running at all...if there is an error and the script is running, you will get error messages when running setup:upgrade

I hope that helps!

2
  • 1
    Thank you for your response. If I clearly understand the goal, InstallSchema executes only when there no any values in setup table. If your module is already installed in system - you need make any changes in UpgradeSchema. That because my file didn't execute. When I renamed it to upgrade and make necessary changes - everything started working properly
    – zhartaunik
    Jun 18, 2015 at 16:35
  • I'm sorry, that is 100% correct, step 1 should have been to delete the entry from the setup_module table or to make it an upgrade script. I'm glad it's working for you!
    – Jer_
    Jun 18, 2015 at 16:43
0

Delete entry of module from table 'setup_module' then run the command php bin/magento setup:upgrade. It will work.

0

you can create a script and in you dbscripts folder and run this file from terminal or web browser.

e.g save file in pub/dbscripts/filename.php paste this code and change according to your requirement

<?php
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

$salesTable = $resource->getTableName('Table_Name');
$sql = "ALTER TABLE ".$salesTable. " ADD `Field_name` varchar(255)";
$connection->query($sql);

echo"Script Run Successfully";

run this file from browser like

domain.name/pub/dbscripts/filename.php

Your Answer

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

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