0

I've created a custom module that includes a script (InstallData.php) that adds a new attribute to an existing attribute group (Product Label):

'group' => 'Product Label'

However, when I enable the module and run setup:upgrade, I receive the following error:

Installing data... SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '10-Product Label' for key 'EAV_ATTRIBUTE_GROUP_ATTRIBUTE_SET_ID_ATTRIBUTE_GROUP_NAME', query was: INSERT INTO `eav_attribute_group` (`attribute_set_id`, `attribute_group_name`, `sort_order`, `attribute_group_code`) VALUES (?, ?, ?, ?)

If I run the following database query I can see that there are two records with the group name 'Product Label', however I don't see these as duplicates as they belong to attribute set 10, and 4 respectively.

MariaDB [max]> select attribute_group_name, attribute_set_id from eav_attribute_group where attribute_group_name = 'Product Label';
+----------------------+------------------+
| attribute_group_name | attribute_set_id |
+----------------------+------------------+
| Product Label        |                4 |
| Product Label        |               10 |
+----------------------+------------------+
2 rows in set (0.00 sec)

Furthermore, I'm unable to delete these records as it will cause the attributes that are currently under those attribute labels to lose their current values.

Does anyone know what may be causing this issue? I have verified that the InstallData.php script works, by adding the attribute to the default group, this issue only occurs when adding the attribute to the attribute group 'Product Label'.

1 Answer 1

0
<?php
namespace Vendor\Extension\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
class InstallData implements InstallDataInterface
{
protected $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
    {
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
            Product::ENTITY,
'new_attr',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'New attr',
'input' => 'select',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'apply_to' => ''
]
        );
$attributeId = $eavSetup->getAttributeId(
            Product::ENTITY,
'new_attr'
);
foreach ($eavSetup->getAllAttributeSetIds(Product::ENTITY) as $attributeSetId) {
try {
$attributeGroupId = $eavSetup->getAttributeGroupId(
                    Product::ENTITY,
$attributeSetId,
'General'
);
} catch (\Exception $e) {
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(
                    Product::ENTITY,
$attributeSetId
);
}
$eavSetup->addAttributeToSet(
                Product::ENTITY,
$attributeSetId,
$attributeGroupId,
$attributeId
);
}
    }
}
2
  • Thanks, I modified this code to place the attribute in the 'Product Label' group, however it ends up being added to the default group instead. I further tested by modifying the code to place the attribute in the 'Design' group, this worked without issues. I think I need to figure out why 'Product Label' is being seen as a duplicate at the database level.
    – Max
    Commented Jul 3, 2020 at 1:22
  • Hi max . Did you find why you had the duplicate ? I try to insert 10 000 attributes set by script (with the api) and at one moment i have this duplicate .... :( Commented Feb 7, 2023 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.