Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

Add AUTO-INCREMENT on column using Phinx migration library

We decided to have a migration library for our projects so Phinx was made the library of choice. Migration is a good practice most expecially when working in a team. It can be seen as version control for database schema (more like Git, SVN and the rest).

Phinx allows developers to alter and manipulate databases in a clear and concise way. It avoids the use of writing SQL by hand and instead offers a powerful API for creating migrations using PHP code. Developers can then version these migrations using their preferred SCM system. This makes Phinx migrations portable between different database systems. Phinx keeps track of which migrations have been run so you can worry less about the state of your database and instead focus on building better software.

I ran into an issue where I can not specify a column as auto increment after disabling the default column ID created by Phinx. Funny enough, this case was not used as an example in the documentation. I have also added a case where the column is null. Here is a classic example to help you move on.


<?php

use Phinx\Migration\AbstractMigration;

class CreateAdmins extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('admins', [
            'id' => false,
            'primary_key' => 'admin_id',
        ]);

        $table
            ->addColumn('admin_id', 'biginteger', [
                'limit' => 20,
                'identity' => true
            ])
            ->addColumn('first_name', 'string', [
                'limit' => 50,
            ])
            ->addColumn('last_name', 'string', [
                'limit' => 50,
            ])
            ->addColumn('email', 'string', [
                'limit' => 150,
            ])
            ->addColumn('app_id', 'biginteger', [
                'limit' => 20,
            ])
            ->addColumn('role', 'boolean', [
                'limit' => 4
            ])
            ->addColumn('status', 'enum', [
                'values' => ['active', 'inactive'],
                'default' => 'active',
            ])
            ->addColumn('last_seen', 'datetime', [
                'null' => true
            ])
            ->addColumn('created_at', 'timestamp', [
                'default' => 'CURRENT_TIMESTAMP',
            ])
            ->addColumn('updated_at', 'timestamp', [
                'default' => 'CURRENT_TIMESTAMP',
                'update' => 'CURRENT_TIMESTAMP',
            ])
            ->create();
    }
}

Drop a comment if this is helpful.

 


comments powered by Disqus