Create Unique field in Codeigniter DBForge Migration Create Unique field in Codeigniter DBForge Migration codeigniter codeigniter

Create Unique field in Codeigniter DBForge Migration


There is not an option to do that in the database driver.

either write the SQL to create the table manually, or (and this isn't what i would recommend) change the DB_forge.php and your database driver. (eg mysql_forge.php) to have unique keys. looking at the code i guess you would just have a class variable called 'unique_keys' and follow the code where 'keys', and 'primary keys' are to add unique keys in.

another option is to use the dbforge as written, then just modifiy the table after creation

$this->db->query('ALTER TABLE `financial_year` ADD UNIQUE INDEX (`financial_year`)');


This question is marked as CodeIgniter2 - @pgee70 Answer is correct

but

CodeIgniter3 supports the creation of unique fields.Just add

'unique'            => TRUE

to the field

In this case:

'financial_year'    =>  array(        'type'              =>  'VARCHAR',        'constraint'        =>  20,        'unique'            => TRUE    ),

see CodeIgniter user_guide