0

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

1 Answer 1

0

I figured out the cause of the issue, and it is kind of a confusing thing to have in Magento. In setting up the attribute, unique code is used to set up the attribute. However, this code in the database is written as is_unique so I tried that in the UpgradeData.php and it worked:

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',
                'is_unique', /** <- was 'unique' */
                false,
                null
            );
        }

I was unsure whether Magento accepted the previous code in InstallData.php so I removed the attribute from the database with the module in setup_module and did a set:up command. After that, I checked the database, and the value of is_unique is set the same as the value in InstallData.php.

In summary

Magento accepted unique as the code when calling AddAttribute and did not accept it (without showing any error) when calling UpdateAttribute but accepted is_unique code.

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.