Phinx: Add auto increment property to Column class

Created on 30 May 2013  路  4Comments  路  Source: cakephp/phinx

We can add autoIncrement property to Column class.

Then we can write in PhinxDbAdapterMysqlAdapter (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 ;)

enhancement

Most helpful comment

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

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

All 4 comments

@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:

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings