Question

How to add a new column in admin_user table Magento 2 ?

Was it helpful?

Solution

app/code/vendor/modulename/Setup/UpgradeSchema.php

<?php

namespace Vendor\Module\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){

$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.1') < 0) {

// Get module table

$tableName = $setup->getTable('admin_user');

// Check if the table already exists

if ($setup->getConnection()->isTableExists($tableName) == true) {

// Declare data

$columns = [

'tok_value' => [

'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,

'nullable' => true,

'comment' => 'Token Value',

],

];

$connection = $setup->getConnection();

foreach ($columns as $name => $definition) {

$connection->addColumn($tableName, $name, $definition);

}


}

}


$setup->endSetup();

}

}

Note: If you face any issue, it may be due to the module that you have already installed. As you know, if the module is already installed then setup:upgrade command does not install schema. You will need to look into your setup_module table, delete your module from the table and re-run the php bin/magento setup:upgrade command.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top