title: Laravel 5 Migration Error Related to Foreign Key
body: <p>This is an attempt to learn Laravel (specifically v5.x) by porting an old project using a SQL dump that contains this:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `locationid` int(11) NOT NULL,
    `username` varchar(45) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
    `email` varchar(200) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
    `firstname` varchar(100) NOT NULL,
    `lastname` varchar(100) NOT NULL,
    `password` varchar(255) NOT NULL,
    `active` bit(1) DEFAULT b'1',
    `token` varchar(100) DEFAULT NULL,
    `token_created` timestamp NULL DEFAULT NULL,
    `role` varchar(45) NOT NULL DEFAULT 'user',
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `email_UNIQUE` (`email`),
    UNIQUE KEY `username_UNIQUE` (`username`),
    KEY `location_idx` (`locationid`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  </code></pre>
  
  <p>And at the bottom of the dump had this related to this specific table:</p>
  
  <pre><code>ALTER TABLE `user`
    ADD CONSTRAINT `location_userfk` FOREIGN KEY (`locationid`) REFERENCES `location` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
  </code></pre>
  
  <p>From a couple stackoverflow questions <a href="https://stackoverflow.com/questions/24302397/laravel-migration-foreign-key-issue">1</a>, <a href="https://stackoverflow.com/questions/19847581/foreign-key-constraint-error-when-adding-through-migrating">2</a>, <a href="https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel">3</a>; I've altered my original code to split out the foreign key to Schema::table from Schema::create and added unsigned() to each of the fields:</p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('locationid')-&gt;unsigned();
          // $table-&gt;foreign('locationid')-&gt;references('id')-&gt;on('location');
          $table-&gt;string('username', 60)-&gt;unique();
          $table-&gt;string('email', 200)-&gt;unique();
          $table-&gt;string('firstname', 100);
          $table-&gt;string('lastname', 100);
          $table-&gt;string('password', 255);
          $table-&gt;boolean('active')-&gt;default(TRUE);
          $table-&gt;string('role', 45)-&gt;default('user');
          // $table-&gt;string('token', 255)-&gt;nullable()-&gt;default(NULL);
          // $table-&gt;string('token_create')-&gt;nullable()-&gt;default(NULL);
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  
      Schema::table('users', function(Blueprint $table) 
      {
          $table-&gt;foreign('locationid')-&gt;references('id')-&gt;on('location');
      });
  }
  </code></pre>
  
  <p>But I still get an error when I migrate when using an empty newly created DB:</p>
  
  <pre><code>exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint' in /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php:358
  Stack trace:
  #0 /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php(358): PDOStatement-&gt;execute(Array) etc...
  
  Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[HY000]: General err
  or: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint users_loca
  tionid_foreign foreign key (`locationid`) references `location` (`id`))' in /home/vagrant/proj
  ects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php:614   
  Stack trace:                                                                                  
  #0 /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php(570): Illuminate\Database\Connection-&gt;runQueryCallback('alter table `us...', Ar
  ray, Object(Closure))
  </code></pre>
  
  <p>Which according to <a href="http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_cannot_add_foreign" rel="nofollow noreferrer">MySQL</a> means:</p>
  
  <pre><code>Error: 1215 SQLSTATE: HY000 (ER_CANNOT_ADD_FOREIGN)
  Message: Cannot add foreign key constraint                                         
  </code></pre>
  
  <p>After which I can't rollback or migrate again without getting another type of error regarding the existence of users table already in the DB so each time I try a version of the above code I've been dropping my DB each time.</p>
  
  <p>I'm sure after fixing this it will occur in all the other tables, which are similar or have more than one foreign key so hopefully whatever fixes this will work for the rest.</p>