phpmagentoinstallationauto-increment

Add an auto_increment column in Magento setup script without using SQL


Previously I asked how to ALTER TABLE in Magento setup script without using SQL. There, Ivan gave an excellent answer which I still refer to even now.

However I have yet to discover how to use Varien_Db_Ddl_Table::addColumn() to specify an auto_increment column. I think it has something to do with an option called identity but so far have had no luck.

Is this even possible or is that functionality incomplete?


Solution

  • One can create an autoincrement column like that (at least since Magento 1.6, maybe even earlier):

    /** @var $table Varien_Db_Ddl_Table */
    $table->addColumn( 'id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'auto_increment' => true,
        'unsigned' => true,
        'nullable' => false,
        'primary' => true,
    ), 'ID' );
    

    Instead of "auto_increment", one may also use the keyword "identity".