Add auto increment property to Column class
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, 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'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