I need to store relationships where a single attribute_id may be visible in multiple store_id and I was trying to use attribute_id as the primary key. It would be something like this:

| attribute_id | store_id |
| 147          | 1        |
| 147          | 2        |

But Magento doesn't see that with good eyes as it returns the exception Item (foo) with the same ID "x" already exists.

What is the best solution for that problem?

  • I think you can't do that as Primary Key cannot be duplicated in any database. – fmsthird Apr 21 '19 at 18:44
  • 1
    the best solution is to create new ID as primary key – fmsthird Apr 21 '19 at 18:47
  • @magefms when I tried setting both of them as primary keys, the database accepted it, but not Magento... Anyway, I've thought about creating a new ID as primary key, but couldn't get a solution on how to update the relationship when it has too.. It is worth saying that I don't have much experience with databases management so if you know some technique or has some link to share on how to deal with this situation, I would be thankful. But thanks anyway – rodrigoriome Apr 21 '19 at 19:07

Here is what I did, I created a relationship id column (like @magefms recommended) which happens to be the primary key, and whenever I need to update this relationship, I delete all the records that contains an specific attribute_id and create it again. To do that I implemented the following function in my model:

public function deleteByColumnValue($column, $value)
    $collection = $this->getCollection()->addFieldToFilter($column, $value);
    foreach($collection as $item) {

This function is probably available somewhere across Mage_Core, but I couldn't find it. Anyway, then on the controller save action, this is how I call it:

Mage::getModel('module/model')->deleteByColumnValue('attribute_id', $storeId);

Hope this is helps someone!

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.