Posted on

Cannot Update EAV Unique Attribute

I have a custom product attribute custom_attr that I created using InstallData.php and set its unique value to true. Now, I am trying to set it back to false in UpgradeData.php but the changes are not working. What could be wrong with my code? I have checked the database table eav_attribute and the is_unique field is not updated to 0.

Magento version: 2.4.3-p1

InstallData.php:

namespace SomeVendorAModuleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

$eavSetup = $this->eavSetupFactory->create([‘setup’ => $setup]);

$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
‘custom_attr’,
[
‘type’ => ‘int’,
‘backend’ => ”,
‘frontend’ => ”,
‘label’ => ‘Enable Custom Attr’,
‘input’ => ‘boolean’,
‘class’ => ”,
‘source’ => ‘MagentoEavModelEntityAttributeSourceBoolean’,
‘global’ => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
‘visible’ => true,
‘required’ => false,
‘user_defined’ => false,
‘default’ => ”,
‘searchable’ => false,
‘filterable’ => false,
‘comparable’ => false,
‘visible_on_front’ => false,
‘used_in_product_listing’ => false,
‘unique’ => false, /** <- this was true */
‘apply_to’ => ‘simple’,
]
);

}
}

UpgradeData.php:

namespace SomeVendorAModuleSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if ($context->getVersion() && version_compare($context->getVersion(), ‘1.1.0’) < 0) {

$eavSetup = $this->eavSetupFactory->create([‘setup’ => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
‘custom_attr’,
[
‘type’ => ‘int’,
‘backend’ => ”,
‘frontend’ => ”,
‘label’ => ‘Enable Commerce Connector Tag’,
‘input’ => ‘boolean’,
‘class’ => ”,
‘source’ => ‘MagentoEavModelEntityAttributeSourceBoolean’,
‘global’ => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
‘visible’ => true,
‘required’ => false,
‘user_defined’ => false,
‘default’ => ”,
‘searchable’ => false,
‘filterable’ => false,
‘comparable’ => false,
‘visible_on_front’ => false,
‘used_in_product_listing’ => false,
‘unique’ => false, /** <- this was true */
‘apply_to’ => ‘simple’,
]
);
}

if ($context->getVersion() && version_compare($context->getVersion(), ‘1.1.1’) < 0) {

$eavSetup = $this->eavSetupFactory->create([‘setup’ => $setup]);
$eavSetup->updateAttribute(
MagentoCatalogModelProduct::ENTITY,
‘custom_attr’,
‘unique’,
false,
null
);
}

$setup->endSetup();
}
}

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=”SomeVendor_AModule” setup_version=”1.1.1″ />
</config>

After making the changes I ran the commands:

bin/magento set:up
bin/magento set:di:com
bin/magento s:s:d -f
bin/magento ind:rei
bin/magento c:f

Leave a Reply

Your email address will not be published.