Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 921
clockworkgeek Avatar asked Mar 17 '11 16:03

clockworkgeek


People also ask

How do I add auto increment to a column?

Here's the SQL statement to add AUTO INCREMENT constraint to id column. ALTER TABLE sales MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY; Next we will add a couple of rows in sales table. As you can see, the MySQL has automatically increased and populated id column with values 7 and 8.

Can you set auto increment?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Can we have two auto increment columns?

You can't have two auto-increment columns.

How do you auto increment a table?

You can also make an auto increment in SQL to start from another value with the following syntax: ALTER TABLE table_name AUTO_INCREMENT = start_value; In the syntax above: start_value: It is the value from where you want to begin the numbering.


1 Answers

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".

like image 125
feeela Avatar answered Oct 30 '22 09:10

feeela