Question

I have created an EAV module and when I run the install script the following error appears.

Error in file: "/var/www/html/carparks/app/code/local/Easyinnovates/Carparks/sql/easyinnovates_carparks_setup/install-1.0.0.php" - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '9-name' for key 'UNQ_EAV_ATTRIBUTE_ENTITY_TYPE_ID_ATTRIBUTE_CODE', query was: INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Trace:
#0 /var/www/html/carparks/app/code/core/Mage/Core/Model/Resource/Setup.php(644): Mage::exception('Mage_Core', 'Error in file: ...')
#1 /var/www/html/carparks/app/code/core/Mage/Core/Model/Resource/Setup.php(421): Mage_Core_Model_Resource_Setup->_modifyResourceDb('install', '', '1.0.0')
#2 /var/www/html/carparks/app/code/core/Mage/Core/Model/Resource/Setup.php(327): Mage_Core_Model_Resource_Setup->_installResourceDb('1.0.0')
#3 /var/www/html/carparks/app/code/core/Mage/Core/Model/Resource/Setup.php(235): Mage_Core_Model_Resource_Setup->applyUpdates()
#4 /var/www/html/carparks/app/code/core/Mage/Core/Model/App.php(428): Mage_Core_Model_Resource_Setup::applyAllUpdates()
#5 /var/www/html/carparks/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Model_App->_initModules()
#6 /var/www/html/carparks/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#7 /var/www/html/carparks/index.php(83): Mage::run('', 'store')
#8 {main}

I know that shown query in above error message is wrong. I have no clue how to figure it.

My install script is as below.

<?php

$this->startSetup();

//create the entity table
$table = $this->getConnection()
                ->newTable($this->getTable('easyinnovates_carparks/carpark'))
                ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
                        'identity'  => true,
                        'unsigned'  => true,
                        'nullable'  => false,
                        'primary'   => true,
                ), 'Entity ID')
                ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
                        'unsigned'  => true,
                        'nullable'  => false,
                        'default'   => '0',
                ), 'Entity Type ID')
                ->addColumn('attribute_set_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
                        'unsigned'  => true,
                        'nullable'  => false,
                        'default'   => '0',
                ), 'Attribute Set ID')
                ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
                ), 'Creation Time')
                ->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
                ), 'Update Time')
                ->addIndex($this->getIdxName('easyinnovates_carparks/carpark', array('entity_type_id')),
                        array('entity_type_id'))
                        ->addIndex($this->getIdxName('easyinnovates_carparks/carpark', array('attribute_set_id')),
                                array('attribute_set_id'))
                                ->addForeignKey(
                                        $this->getFkName(
                                                'easyinnovates_carparks/carpark',
                                                'attribute_set_id',
                                                'eav/attribute_set',
                                                'attribute_set_id'
                                        ),
                                        'attribute_set_id', $this->getTable('eav/attribute_set'), 'attribute_set_id',
                                        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
                                        ->addForeignKey($this->getFkName('easyinnovates_carparks/carpark', 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
                                                'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
                                                Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
                                                ->setComment('Carpark Table');
$this->getConnection()->createTable($table);

//create the attribute values tables (int, decimal, varchar, text, datetime)
$articleEav = array();
$articleEav['int'] = array(
        'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
        'length'    => null,
        'comment'   => 'Carpark Datetime Attribute Backend Table'
);

$articleEav['varchar'] = array(
        'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
        'length'    => 255,
        'comment'   => 'Carpark Varchar Attribute Backend Table'
);

$articleEav['text'] = array(
        'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
        'length'    => '64k',
        'comment'   => 'Carpark Text Attribute Backend Table'
);

$articleEav['datetime'] = array(
        'type'      => Varien_Db_Ddl_Table::TYPE_DATETIME,
        'length'    => null,
        'comment'   => 'Carpark Datetime Attribute Backend Table'
);

$articleEav['decimal'] = array(
        'type'      => Varien_Db_Ddl_Table::TYPE_DECIMAL,
        'length'    => '12,4',
        'comment'   => 'Carpark Datetime Attribute Backend Table'
);

foreach ($articleEav as $type => $options) {
    $table = $this->getConnection()
    ->newTable($this->getTable(array('easyinnovates_carparks/carpark', $type)))
    ->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'identity'  => true,
            'nullable'  => false,
            'primary'   => true,
    ), 'Value ID')
    ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
    ), 'Entity Type ID')
    ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
    ), 'Attribute ID')
    ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
    ), 'Store ID')
    ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
    ), 'Entity ID')
    ->addColumn('value', $options['type'], $options['length'], array(
    ), 'Value')
    ->addIndex(
            $this->getIdxName(
                    array('easyinnovates_carparks/carpark', $type),
                    array('entity_id', 'attribute_id', 'store_id'),
                    Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE
            ),
            array('entity_id', 'attribute_id', 'store_id'),
            array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
            ->addIndex($this->getIdxName(array('easyinnovates_carparks/carpark', $type), array('store_id')),
                    array('store_id'))
                    ->addIndex($this->getIdxName(array('easyinnovates_carparks/carpark', $type), array('entity_id')),
                            array('entity_id'))
                            ->addIndex($this->getIdxName(array('easyinnovates_carparks/carpark', $type), array('attribute_id')),
                                    array('attribute_id'))
                                    ->addForeignKey(
                                            $this->getFkName(
                                                    array('easyinnovates_carparks/carpark', $type),
                                                    'attribute_id',
                                                    'eav/attribute',
                                                    'attribute_id'
                                            ),
                                            'attribute_id', $this->getTable('eav/attribute'), 'attribute_id',
                                            Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
                                            ->addForeignKey(
                                                    $this->getFkName(
                                                            array('easyinnovates_carparks/carpark', $type),
                                                            'entity_id',
                                                            'easyinnovates_carparks/carpark',
                                                            'entity_id'
                                                    ),
                                                    'entity_id', $this->getTable('easyinnovates_carparks/carpark'), 'entity_id',
                                                    Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
                                                    ->addForeignKey($this->getFkName(array('easyinnovates_carparks/carpark', $type), 'store_id', 'core/store', 'store_id'),
                                                            'store_id', $this->getTable('core/store'), 'store_id',
                                                            Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
                                                            ->setComment($options['comment']);
    $this->getConnection()->createTable($table);
}
//crete the carparks_eav_attribute (for additional attribute settings)
$table = $this->getConnection()
            ->newTable($this->getTable('easyinnovates_carparks/eav_attribute'))
            ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
                    'identity'  => true,
                    'nullable'  => false,
                    'primary'   => true,
            ), 'Attribute ID')
            ->addColumn('is_global', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute scope')
            ->addColumn('position', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute position')
            ->addColumn('is_wysiwyg_enabled', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute uses WYSIWYG')
            ->addColumn('is_visible', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(), 'Attribute is visible')
            ->setComment('Carparks attribute table');

$this->getConnection()->createTable($table);

$this->installEntities();
$this->endSetup();

In the above install script if I committed the line before last line $this->installEntities(); , then script runs without any error. But it will create all the tables empty. I'm not sure what is the exact error.

My config.xml is as below.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Easyinnovates_Carparks>
            <version>1.0.0</version>
        </Easyinnovates_Carparks>
    </modules>
    <global>
        <resources>
            <easyinnovates_carparks_setup>
                <setup>
                    <module>Easyinnovates_Carparks</module>
                    <class>Easyinnovates_Carparks_Model_Resource_Setup</class>
                </setup>
            </easyinnovates_carparks_setup>
        </resources>
        <blocks>
            <easyinnovates_carparks>
                <class>Easyinnovates_Carparks_Block</class>
            </easyinnovates_carparks>
        </blocks>
        <helpers>
            <easyinnovates_carparks>
                <class>Easyinnovates_Carparks_Helper</class>
            </easyinnovates_carparks>
        </helpers>
        <models>
            <easyinnovates_carparks>
                <class>Easyinnovates_Carparks_Model</class>
                <resourceModel>easyinnovates_carparks_resource</resourceModel>
            </easyinnovates_carparks>
            <easyinnovates_carparks_resource>
                <class>Easyinnovates_Carparks_Model_Resource</class>
                <entities>
                    <carpark>
                        <table>easyinnovates_carparks_carpark</table>
                    </carpark>
                    <eav_attribute>
                        <table>easyinnovates_carparks_eav_attribute</table>
                    </eav_attribute>
                </entities>
            </easyinnovates_carparks_resource>
        </models>
        <events>
            <controller_front_init_routers><!-- event for custom router - url rewrites -->
                <observers>
                    <easyinnovates_carparks>
                        <class>Easyinnovates_Carparks_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </easyinnovates_carparks>
                </observers>
            </controller_front_init_routers>
        </events>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <easyinnovates_carparks>
                    <file>easyinnovates_carparks.xml</file>
                </easyinnovates_carparks>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easyinnovates_Carparks>
                    <files>
                        <default>Easyinnovates_Carparks.csv</default>
                    </files>
                </Easyinnovates_Carparks>
            </modules>
        </translate>
        <global_search>
            <carpark>
                <class>easyinnovates_carparks/adminhtml_search_carpark</class>
                <acl>easyinnovates_carparks</acl>
            </carpark>
        </global_search>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Easyinnovates_Carparks before="Mage_Adminhtml">Easyinnovates_Carparks_Adminhtml</Easyinnovates_Carparks>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <frontend>
        <events>
            <page_block_html_topmenu_gethtml_before><!-- add link to top menu -->
                <observers>
                    <easyinnovates_carparks>
                        <class>easyinnovates_carparks/observer</class>
                        <method>addItemsToTopmenuItems</method>
                    </easyinnovates_carparks>
                </observers>
            </page_block_html_topmenu_gethtml_before>
        </events>

        <routers>
            <easyinnovates_carparks>
                <use>standard</use>
                <args>
                    <module>Easyinnovates_Carparks</module>
                    <frontName>carparks</frontName>
                </args>
            </easyinnovates_carparks>
        </routers>
        <layout>
            <updates>
                <easyinnovates_carparks>
                    <file>easyinnovates_carparks.xml</file>
                </easyinnovates_carparks>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easyinnovates_Carparks>
                    <files>
                        <default>Easyinnovates_Carparks.csv</default>
                    </files>
                </Easyinnovates_Carparks>
            </modules>
        </translate>
    </frontend>
    <default>
        <easyinnovates_carparks>
            <carpark>
                <breadcrumbs>1</breadcrumbs>
                <url_prefix>carpark</url_prefix>
                <url_suffix>html</url_suffix>
                <rss>1</rss>
                <meta_title>Carparks</meta_title>
            </carpark>
        </easyinnovates_carparks>
    </default>
</config>

Can anyone figure out what exactly the error is?

Thanks in advanced.

EDIT:

The function getDefaultEntities() looks like below.

public function getDefaultEntities()
    {
        $entities = array();
        $entities['easyinnovates_carparks_carpark'] = array(
                'entity_model'                  => 'easyinnovates_carparks/carpark',
                'attribute_model'               => 'easyinnovates_carparks/resource_eav_attribute',
                'table'                         => 'easyinnovates_carparks/carpark',
                'additional_attribute_table'    => 'easyinnovates_carparks/eav_attribute',
                'entity_attribute_collection'   => 'easyinnovates_carparks/carpark_attribute_collection',
                'attributes'        => array(
                        'name' => array(
                                'group'          => 'General',
                                'type'           => 'varchar',
                                'backend'        => '',
                                'frontend'       => '',
                                'label'          => 'Name',
                                'input'          => 'text',
                                'source'         => '',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                                'required'       => '1',
                                'user_defined'   => false,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '10',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '0',
                        ),
                        'short_description' => array(
                                'group'          => 'General',
                                'type'           => 'text',
                                'backend'        => '',
                                'frontend'       => '',
                                'label'          => 'Short description',
                                'input'          => 'textarea',
                                'source'         => '',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                                'required'       => '1',
                                'user_defined'   => true,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '20',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '0',
                        ),
                        'description' => array(
                                'group'          => 'General',
                                'type'           => 'text',
                                'backend'        => '',
                                'frontend'       => '',
                                'label'          => 'Description',
                                'input'          => 'textarea',
                                'source'         => '',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                                'required'       => '1',
                                'user_defined'   => true,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '30',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '1',
                        ),
                        'publish_date' => array(
                                'group'          => 'General',
                                'type'           => 'datetime',
                                'backend'        => 'eav/entity_attribute_backend_datetime',
                                'frontend'       => '',
                                'label'          => 'Publish Date',
                                'input'          => 'date',
                                'source'         => '',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                                'required'       => '1',
                                'user_defined'   => true,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '40',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '0',
                        ),
                        'status' => array(
                                'group'          => 'General',
                                'type'           => 'int',
                                'backend'        => '',
                                'frontend'       => '',
                                'label'          => 'Enabled',
                                'input'          => 'select',
                                'source'         => 'eav/entity_attribute_source_boolean',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                                'required'       => '',
                                'user_defined'   => false,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '50',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '0',
                        ),
                        'meta_title' => array(
                                'group'          => 'General',
                                'type'           => 'varchar',
                                'backend'        => '',
                                'frontend'       => '',
                                'label'          => 'Meta title',
                                'input'          => 'text',
                                'source'         => '',
                                'global'         => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                                'required'       => '',
                                'user_defined'   => false,
                                'default'        => '',
                                'unique'         => false,
                                'position'       => '80',
                                'note'           => '',
                                'visible'        => '1',
                                'wysiwyg_enabled'=> '0',
                        )
                    )
                );
            return $entities;
    }       
Was it helpful?

Solution

Most probably you already executed this script once before and your entities and attributes were already installed.
You need to run these queries on the db before running the install script again (backup your db in case I'm wrong):

DELETE FROM 
    eav_attribute 
WHERE 
    entity_type_id = (
                      SELECT 
                          entity_type_id 
                      FROM 
                          eav_entity_type 
                      WHERE 
                          entity_type_code = 'easyinnovates_carparks_carpark'
                     )

This will delete the attributes.
Now delete the entity type:

DELETE FROM 
    eav_entity_type 
WHERE 
    entity_type_code = 'easyinnovates_carparks_carpark'

Now clear the cache and try again.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top