title: I am using db forge for database migration.add_foreign_key add keys from first created table to second one
body: <p>I am using db forge for  database migration in ci.When I add foreign key in first created table. It works but in second table foreign key from first table inherit so it create problem to create the sencond table. Here is  my code</p>
  
  <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  class Initial_schema {
      function up() 
      {
          $CI =&amp; get_instance();
          if($CI-&gt;migrate-&gt;verbose)
              echo "Creating tables...";
          // create table TblRowStatusEnum
          if(! $CI-&gt;db-&gt;table_exists('TblRowStatusEnum')) {
              $cols = array(
                  'id' =&gt; array('type' =&gt; 'INT', 'constraint' =&gt; 11, 'auto_increment' =&gt; TRUE,'null'=&gt;FALSE),
                  'status' =&gt; array('type' =&gt; 'enum("Active","Inactive","Delete")', 'null' =&gt; FALSE),
                  'description' =&gt; array('type' =&gt; 'VARCHAR', 'constraint' =&gt; '200', 'null' =&gt; True),
                  'createdAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; FALSE),
                  'updatedAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; TRUE)
  
              );
  
              // Setup Keys
              $CI-&gt;dbforge-&gt;add_key('id', TRUE);
              $CI-&gt;dbforge-&gt;add_field($cols);
              $CI-&gt;dbforge-&gt;create_table('TblRowStatusEnum', TRUE);
  
              //insert data
              /*$insert = array('status' =&gt; 'Active','description' =&gt;'Active');
              $CI-&gt;db-&gt;insert('TblRowStatusEnum', $insert);
              $insert = array('status' =&gt; 'Inactive','description' =&gt;'Inactive');
              $CI-&gt;db-&gt;insert('TblRowStatusEnum', $insert);
              $insert = array('status' =&gt; 'Delete','description' =&gt;'Delete');
              $CI-&gt;db-&gt;insert('TblRowStatusEnum', $insert);*/
  
      }
  
      // create table TblUser
      if(! $CI-&gt;db-&gt;table_exists('TblUser')) {
          $cols = array(
          'id' =&gt; array('type' =&gt; 'INT', 'constraint' =&gt; 11,'auto_increment' =&gt; TRUE,'null'=&gt;FALSE),
          'facebookId' =&gt; array('type' =&gt; 'INT','constraint' =&gt; '11', 'null' =&gt; FALSE),
          'firstName' =&gt; array('type' =&gt; 'VARCHAR','constraint' =&gt; '255', 'null' =&gt; FALSE),
          'middleName' =&gt; array('type' =&gt; 'VARCHAR','constraint' =&gt; '255', 'null' =&gt; TRUE),
          'lastName' =&gt; array('type' =&gt; 'VARCHAR','constraint' =&gt; '255', 'null' =&gt; FALSE),
          'email' =&gt; array('type' =&gt; 'VARCHAR','constraint' =&gt; '255', 'null' =&gt; FALSE),
          'address' =&gt; array('type' =&gt; 'TEXT'),
          'dateOfBirth' =&gt; array('type' =&gt; 'DATE'),
          'facebookAuthToken' =&gt; array('type' =&gt; 'TEXT', 'null' =&gt; FALSE ,'comment'=&gt;"For storing auth token for offline access. If we don''t need offline access this column can be deleted"),
          'profileImageUrl' =&gt; array('type' =&gt; 'TEXT','null' =&gt; FALSE),
          'rowStatus' =&gt; array('type' =&gt; 'INT','constraint' =&gt; '11','null' =&gt; FALSE),
          'createdAt' =&gt; array('type' =&gt; 'DATETIME', 'null' =&gt; FALSE),
          'updatedAt' =&gt; array('type' =&gt; 'DATETIME', 'null' =&gt; TRUE)
          );
  
          // Setup Keys
          $CI-&gt;dbforge-&gt;add_key('id', TRUE);
          $CI-&gt;dbforge-&gt;add_key('rowStatus');
          $CI-&gt;dbforge-&gt;add_field($cols);
  
  
  
  
          //gives CONSTRAINT FOREIGN KEY `fk_testTable_testField` (`myField`)
          //      REFERENCES testTable(`testField`) ON DELETE CASCADE ON UPDATE CASCADE
  
          $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'rowStatus',
          'foreign_table' =&gt; 'TblRowStatusEnum',
          'foreign_field' =&gt; 'id'));
          $CI-&gt;dbforge-&gt;create_table('TblUser', TRUE);
      }
  
      //TblSellingItem
      if(! $CI-&gt;db-&gt;table_exists('TblSellingItem')) {
          $cols = array(
          'id' =&gt; array('type' =&gt; 'INT', 'constraint' =&gt; 11,'auto_increment' =&gt; TRUE,'null'=&gt;FALSE),
          'title' =&gt; array('type' =&gt; 'VARCHAR','constraint' =&gt; '255', 'null' =&gt; FALSE),
          'description' =&gt; array('type' =&gt; 'TEXT'),
          'price' =&gt; array('type' =&gt; 'FLOAT','null' =&gt; FALSE),
          'postedBy' =&gt; array('type' =&gt; 'INT','constraint' =&gt; 11,'null' =&gt; FALSE),
          'rowStatus' =&gt; array('type' =&gt; 'INT','constraint' =&gt; 11,'null' =&gt; FALSE),
          'createdAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; FALSE,'default'=&gt;get_now()),
          'updatedAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; TRUE)
  
          );
  
          // Setup Keys
          $CI-&gt;dbforge-&gt;add_key('id', TRUE);
          $CI-&gt;dbforge-&gt;add_key('postedBy');
          $CI-&gt;dbforge-&gt;add_key('rowStatus');
          $CI-&gt;dbforge-&gt;add_field($cols);
          $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'rowStatus',
             'foreign_table' =&gt; 'TblRowStatusEnum',
             'foreign_field' =&gt; 'id'));
         $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'postedBy',
         'foreign_table' =&gt; 'TblUser',
         'foreign_field' =&gt; 'id'));
          $CI-&gt;dbforge-&gt;create_table('TblSellingItem', TRUE);
  
  
      }
  
      //TblItemImage
      if(! $CI-&gt;db-&gt;table_exists('TblItemImage')) {
          $cols = array(
          'id' =&gt; array('type' =&gt; 'INT', 'constraint' =&gt; 11, 'auto_increment' =&gt; TRUE,'null'=&gt;FALSE),
          'sellingItemId' =&gt; array('type' =&gt; 'INT','constraint' =&gt; '11', 'null' =&gt; FALSE),
          'url' =&gt; array('type' =&gt; 'TEXT','null' =&gt; FALSE),
          'imageOrder' =&gt; array('type' =&gt; 'INT','null' =&gt; FALSE),
          'rowStatus' =&gt; array('type' =&gt; 'INT','constraint' =&gt; 11,'null' =&gt; FALSE),
          'createdAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; FALSE,'default'=&gt;get_now()),
          'updatedAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; TRUE)
  
          );
  
          // Setup Keys
          $status =array('field' =&gt; 'rowStatus',
          'foreign_table' =&gt; 'TblRowStatusEnum',
          'foreign_field' =&gt; 'id');
          $posted_by =array('field' =&gt; 'sellingItemId',
          'foreign_table' =&gt; 'TblSellingItem',
          'foreign_field' =&gt; 'id');
          $CI-&gt;dbforge-&gt;add_key('id', TRUE);
          $CI-&gt;dbforge-&gt;add_key('sellingItemId');
          $CI-&gt;dbforge-&gt;add_key('rowStatus');
          $CI-&gt;dbforge-&gt;add_field($cols);
          $CI-&gt;dbforge-&gt;add_foreign_key($status);
          $CI-&gt;dbforge-&gt;add_foreign_key($posted_by);
          $CI-&gt;dbforge-&gt;create_table('TblItemImage', TRUE);
  
  
  
      }
  
  
      //TblChatMessage
      if(! $CI-&gt;db-&gt;table_exists('TblChatMessage')) {
  
      $cols = array(
      'id' =&gt; array('type' =&gt; 'INT', 'constraint' =&gt; 11, 'auto_increment' =&gt; TRUE,'null'=&gt;FALSE),
      'sellingItemId' =&gt; array('type' =&gt; 'INT','constraint' =&gt; '11', 'null' =&gt; FALSE),
      'message' =&gt; array('type' =&gt; 'TEXT'),
      'sender' =&gt; array('type' =&gt; 'INT','null' =&gt; FALSE),
      'receiver' =&gt; array('type' =&gt; 'INT','null' =&gt; FALSE),
      'sentTime' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; FALSE),
      'messageStatus' =&gt; array('type' =&gt; 'INT','constraint' =&gt; 11,'null' =&gt; FALSE),
      'rowStatus' =&gt; array('type' =&gt; 'INT','constraint' =&gt; 11,'null' =&gt; FALSE),
      'createdAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; FALSE,'default'=&gt;get_now()),
      'updatedAt' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; TRUE)
  
      );
  
      // Setup Keys
      $CI-&gt;dbforge-&gt;add_key('id', TRUE);
      $CI-&gt;dbforge-&gt;add_key('sellingItemId');
      $CI-&gt;dbforge-&gt;add_key('sender');
      $CI-&gt;dbforge-&gt;add_key('receiver');
      $CI-&gt;dbforge-&gt;add_key('rowStatus');
      $CI-&gt;dbforge-&gt;add_field($cols);
  
      $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'rowStatus',
      'foreign_table' =&gt; 'TblRowStatusEnum',
      'foreign_field' =&gt; 'id'));
      $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'sellingItemId',
      'foreign_table' =&gt; 'TblSellingItem',
      'foreign_field' =&gt; 'id'));
      $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'receiver',
      'foreign_table' =&gt; 'TblUser',
      'foreign_field' =&gt; 'id'));
      $CI-&gt;dbforge-&gt;add_foreign_key(array('field' =&gt; 'sender',
      'foreign_table' =&gt; 'TblUser',
      'foreign_field' =&gt; 'id'));
  
      $CI-&gt;dbforge-&gt;create_table('TblChatMessage', TRUE);
  
  
  
  
  
      }
  
  
      }
  
      function down() 
      {
          $CI =&amp; get_instance();
          if($CI-&gt;migrate-&gt;verbose)
          echo "Dropping table accounts...";
          $CI-&gt;dbforge-&gt;drop_table('TblRowStatusEnum');
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Error is like this</p>
  
  <pre><code>A Database Error Occurred
  
  Error Number: 1072
  
  Key column 'postedBy' doesn't exist in table
  
  CREATE TABLE IF NOT EXISTS `TblItemImage` ( `id` INT(11) NOT NULL AUTO_INCREMENT, 
  `sellingItemId` INT(11) NOT NULL, `url` TEXT NOT NULL, `imageOrder` INT NOT NULL, 
  `rowStatus` INT(11) NOT NULL, `createdAt` datetime DEFAULT '2014-03-19 11:51:13' NOT NULL, 
  `updatedAt` datetime NULL, PRIMARY KEY `id` (`id`), FOREIGN KEY `rowStatus` (`rowStatus`) 
  REFERENCES `TblRowStatusEnum`(`id`), FOREIGN KEY `rowStatus` (`rowStatus`) REFERENCES 
  `TblRowStatusEnum`(`id`), FOREIGN KEY `postedBy` (`postedBy`) REFERENCES `TblUser`(`id`), 
  FOREIGN KEY `rowStatus` (`rowStatus`) REFERENCES `TblRowStatusEnum`(`id`), FOREIGN KEY 
  `sellingItemId` (`sellingItemId`) REFERENCES `TblSellingItem`(`id`), KEY `sellingItemId` 
  (`sellingItemId`), KEY `rowStatus` (`rowStatus`) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
  
  Filename: C:\xampp\htdocs\plunder_migration\system\database\DB_driver.php
  
  Line Number: 330
  </code></pre>
  
  <p>here FOREIGN KEY <code>postedBy</code> is add automatically in table TblItemImage. what is wrong with my code? Thank You.</p>