Search code examples
mysqldatabasecodeignitermigrationcodeigniter-3

How can we add column in existing table with migration in CodeIgniter 3



<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_dummy_field_to_blog_table  extends CI_Migration {

        public function up()
        {
                $fields = array(
                        'dummy' => array('type' => 'TEXT')
                );
                $this->dbforge->add_column('blog', $fields);
        }

        public function down()
        {
                $this->dbforge->drop_column('blog', 'dummy');
        }
}

Solution

  • First of all you need to set constraint and then specify the place where you want to add the column. Try this:

    public function up()
            {
                    $fields = array(
                            'dummy' => array(
                                     'type' => 'varchar'
                                     'constraint' => 100,
                                     'after' => 'username'
                        )
                    );
                    $this->dbforge->add_column('blog', $fields);
            }