Geoff
Geoff

Reputation: 6639

Creating foreign key in codeigniter migration fails

Am trying to add a foreign key in codeigniter migration via

i have two tables

tbl_doc_types
   id
   name

tbl_docs
  id
  doc_type //foreign key to tbl_doc_types

So in my migration

class Migration_Add_Tbl_Legal_Documents extends CI_Migration
 {

  public function up()
   {
    $this->dbforge->add_field(array(

         'doc_type' => array(
            'type' => 'INT',
        ),

     $this->dbforge->add_field('CONSTRAINT FOREIGN KEY (doc_type) REFERENCES tbl_doc_types(id)');

But now am getting an error that

Foreign key constraint is incorrectly formed")

Wherer am i going wrong?

Upvotes: 1

Views: 1049

Answers (1)

Gerson E. Aguirre
Gerson E. Aguirre

Reputation: 877

try using something like

$this->db->query('ALTER TABLE `table` ADD CONSTRAINT `name_contraint` FOREIGN KEY(`id_table`) REFERENCES table2(`id_table2`) ON DELETE RESTRICT ON UPDATE RESTRICT;');

instead of $this->dbforge->add_field

Upvotes: 4

Related Questions