What code is being used to prevent two products with same sku from being saved, is it at the attribute level (setting it as unique) or is there some other code being used?


It is at the attribute level which B00MER says.

The attribute has "Unique Value" = true.

The attribute backend will extend Mage_Eav_Model_Entity_Attribute_Backend_Abstract which has a validate function which includes a bit of logic like:

    if ($this->getAttribute()->getIsUnique()) {
        if (!$this->getAttribute()->getEntity()->checkAttributeUniqueValue($this->getAttribute(), $object)) {
            $label = $this->getAttribute()->getFrontend()->getLabel();
            throw Mage::exception('Mage_Eav',
                Mage::helper('eav')->__('The value of attribute "%s" must be unique', $label)

The checkAttributeUniqueValue will do a bit of logic which will query the database to see if there is an entity with the attribute already specified like defined. (The below snippet is edited for brevity, removed some of the EAV logic and just left it looking at static attributes)

public function checkAttributeUniqueValue(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $object)
    $adapter = $this->_getReadAdapter();
    $select = $adapter->select();

    $value = $object->getData($attribute->getAttributeCode());
    $bind = array(
        'entity_type_id' => $this->getTypeId(),
        'attribute_code' => trim($value)

        ->from($this->getEntityTable(), $this->getEntityIdField())
        ->where('entity_type_id = :entity_type_id')
        ->where($attribute->getAttributeCode() . ' = :attribute_code');

    $data = $adapter->fetchCol($select, $bind);

    if ($object->getId()) {
        if (isset($data[0])) {
            return $data[0] == $object->getId();
        return true;

    return !count($data);

This querying of the database means that the unique property of the attribute is not enforced at the database layer. This means that if you have race conditions with reading or writing, it is very possible to get products with duplicate SKUs in magento.

  • where does this validate function get called, what triggers it – pzirkind Dec 24 '15 at 17:24
  • 1
    Pretty sure the adminhtml catalog product controller will call it on the validate action. – Luke Rodgers Dec 24 '15 at 17:40

From my understanding it is only at the attribute level.

Screenshot of attribute

  • Can confirm, it's even possible to change it, but no, that's not a good idea – Fabian Schmengler Dec 24 '15 at 21:29

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.