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 SomeVendor\AModule\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
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(
\Magento\Catalog\Model\Product::ENTITY,
'custom_attr',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Enable Custom Attr',
'input' => 'boolean',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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 SomeVendor\AModule\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
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(
\Magento\Catalog\Model\Product::ENTITY,
'custom_attr',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Enable Commerce Connector Tag',
'input' => 'boolean',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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(
\Magento\Catalog\Model\Product::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