Table Creation In Magento 1.6/1.11+

Posted About 12 years ago. Visible to the public.

Magento CE 1.6 and PE/EE 1.11 introduced database abstraction, this is the way to set up tables using the new code.

$table = $installer->getConnection()
    ->newTable($installer->getTable('your_module/table_name_in_xml'))
    ->addColumn(
        'entity_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'identity' => true,
            'unsigned' => true,
            'nullable' => false,
            'primary'  => true,
        ),
        'Entity ID'
    )
    ->addColumn(
        'category_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'unsigned' => true,
            'nullable' => false,
        ),
        'Category ID'
    )
    ->addIndex(
        $installer->getIdxName(
            'your_module/table_name_in_xml',
            array('category_id')
        ),
        array('category_id')
    )
    ->addForeignKey(
        $installer->getFkName(
            'your_module/table_name_in_xml',
            'category_id',
            'catalog/category',
            'entity_id'
        ),
        'category_id',
        $installer->getTable('catalog/category'),
        'entity_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE,
        Varien_Db_Ddl_Table::ACTION_CASCADE
    )
    ->setComment('Some table which links to categories');

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

Mike Whitby
Last edit
Almost 10 years ago
Posted by Mike Whitby to Magento (2012-02-15 15:21)