phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Add auto increment property to Column class

Open czogori opened this issue 13 years ago • 4 comments

We can add autoIncrement property to Column class.

Then we can write in Phinx\Db\Adapter\MysqlAdapter (line 792)

 $def .= ($column->isIdentity() && $column->isAutoIncrement()) ? ' AUTO_INCREMENT' : '';

instead of

 $def .= ($column->isIdentity()) ? ' AUTO_INCREMENT' : '';

At this point PK is always auto increment.

Btw. for example in Postgres we can add many columns with auto increment ergo this is feature for the future ;)

czogori avatar May 30 '13 20:05 czogori

@czogori, this can be achieved like this:

$table = $this->table('user', array('id' => false));
$table->addColumn('id', 'integer'));

Setting 'id' to false on the table, will avoid creating an auto increment id.


There is, however, still a need to be able to define auto-increment ids on columns. Eg: If we want to change the name, limit or sign of the primary key, then we would have to implement a solution like above.

The issue with doing this is that the field created will no longer be auto-incremented. Also, we should be able to set another key (instead of the primary key) to auto increment if we choose.

I would propose a solution that would allow the user to do this:

$table = $this->table('user', array('id' => false, 'primary_key' => 'id'));
$table->addColumn('id', 'integer', array('limit' => 10, 'signed' => false))
      ->addColumn('banana', 'integer', array('auto_increment' => true, 'limit' => 10))
      ->addIndex('banana', array('unique' => true));

So the user can:

  • rename the primary key
  • change the primary key limit
  • change the primary key to unsigned https://github.com/robmorgan/phinx/pull/156
  • turn auto-increment on or off
  • set auto increment to a different field.

dangerousdan avatar Nov 28 '13 19:11 dangerousdan

dangerousdan's solution is not working. "auto_increment" is not a valid column option.

frazr avatar Jan 13 '16 12:01 frazr

@frazr, you can try use 'identity' => true option:

->addColumn('id', 'integer', array('signed' => false, 'identity' => true))

VladimirCatrici avatar Feb 11 '16 13:02 VladimirCatrici

"identity" is not a valid column option. At least in v0.4.0

Largit avatar Jan 09 '17 19:01 Largit