9

In Magento 2, How to remove a column in customer_entity table?

15

In your setup script, we can use dropColumn:

 $setup->getConnection()->dropColumn($setup->getTable('your_table'), 'your_column');
| improve this answer | |
  • doing with UpgradeSchema.php is a better and cleaner solution – Blueblazer172 Oct 9 '17 at 12:39
7

You can try this simple dropColumn() function in your installer script.

$this->startSetup();

//example: 
$this->getConnection()->dropColumn($this->getTable('your_table_definition'), 'your column name', $schemaName = null)

$this->endSetup();
| improve this answer | |
  • thank your answer! – MrTo-Kane Dec 30 '16 at 8:40
7

Here is my solution, maybe good for referring for other.
I created Tohq\Customer\Setup\UpgradeSchema.php file:

<?php

namespace Tohq\Customer\Setup;

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

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {            
        // Version of module in setup table is less then the give value.
        if (version_compare($context->getVersion(), '0.1.4', '<')) {

            // get table customer_entity
            $eavTable = $setup->getTable('customer_entity');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($eavTable) == true) {
                $connection = $setup->getConnection();

                // del_flg = column name which you want to delete
                $connection->dropColumn($eavTable, 'del_flg');
            }
        }

    }
}
| improve this answer | |
2

Path: Magento22/app/code/Aks/Training/etc/module.xml

<?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="Aks_Training" setup_version="1.0.2"/>
</config>

Create UpgradeSchema.php file under Setup folder in your module.

Path: Magento22/app/code/Aks/Training/Setup/UpgradeSchema.php

<?php
    namespace Aks\Training\Setup;

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

    /**
     * Upgrade the Sales_Order Table to remove extra field
     */
    class UpgradeSchema implements UpgradeSchemaInterface
    {

        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
            if (version_compare($context->getVersion(), '1.0.2', '<')) {
                $setup->getConnection()->dropColumn($setup->getTable('sales_order'), 'gst');
            }
            $setup->endSetup();
        }
    }

I hope It's work for ever

| improve this answer | |

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.