add indexing to database using codeigniter migration?

3.6k views Asked by At

How can i add index to any field in database using codeigniter migration.? I have tried with alter query but i didn't find array key for indexing to pass in alter query?

$fields = array('photo_id' => 
                 array('type' => 'INT',
                       'constraint'=>11,
                       'after'=>'photo_req_id',`enter code here`
                       'null'=>false));

And i also try to find on codeigniter documentation but not mention anything about indexing. Guys please help to set indexing by using codeigniter migration method.

2

There are 2 answers

5
DFriend On BEST ANSWER

Indexing is done by using the add_key function documented here.

To create a PRIMARY KEY index on photo_id

$this->dbforge->add_key('photo_id', TRUE);

To create a secondary index on photo_id

$this->dbforge->add_key('photo_id');
0
Sahib Khan On

There is no such interface until CI 3 and the under development CI 4. You can use

$this->db->query('CREATE INDEX your_index_name ON your_table_name (your_column_name);');