0

I know there are a few posts regarding how to add a new column to the existing table in Magento 2. My concern is if I already have a webshop that is running, can I still add a new column to a table?

for example, I want to add a new column to the table sales_order_item in a webshop database that already has a lot of data existing.

3
  • You should not modify Magento core tables. Especially tables like sales_, quote_, etc. Because some of them already have too many columns. You should introduce your own table and use extension_attributes instead. magento.stackexchange.com/questions/261970/…
    – joni jones
    Feb 10, 2020 at 18:29
  • 1
    While it is true that modifying core tables is poor practice, this is a widely ignored convention. Widely used extensions, including those from from "premium" developers routinely litter the order and quote tables with their own columns. Those tables are ridiculously wide, but that is how it has been for ages and the entire system is built around this legacy. You could invent a better wheel here, but it would take lot of work while providing minimal benefit in terms of efficiency and maintainability. Sometimes the best choice is to do as Magento does, not as the Magento docs say.
    – jiheison
    Feb 10, 2020 at 19:28
  • 1
    and why doing modification on an table is a poor practice, if you are not modifying existing columns, and only adding new ones ? there are some key principles that you need to keep in mind, but lets say he want to add and "prefix_external_reference_number" to "sales_order" table, adding an extra table to save & link the value I would say it's 2 much. The tables you mentioned are that big, for a 1 good reason, and it's called PERFORMANCE, did you ever try to do a complex select with multiple joins on a DB that has few millions records? there is always possible to serialize the value somewhere Jun 9, 2020 at 22:14

1 Answer 1

0

You need to use below cod in your setup script of your custom module

<?php

namespace Vendor\Modulename\Setup;


use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * InstallData
 *
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{


    /**
     * SalesSetupFactory
     *
     * @var SalesSetupFactory
     */
    protected $salesSetupFactory;



    /**
     * Constructor
     * @param SalesSetupFactory $salesSetupFactory
     */
    public function __construct(
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->salesSetupFactory = $salesSetupFactory;
    }


    /**
     * Install
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var \Magento\Sales\Setup\SalesSetup $salesInstaller */
        $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

        $salesInstaller->addAttribute('order_item', 'test', ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'length'=> 10, 'visible' => false,'nullable' => true,]);
    }

}

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.