title: Laravel how to create foreign key constrain from non-primary key
body: <p>I have a table named <code>header</code></p>
  
  <pre><code>$table-&gt;increments('id');
  $table-&gt;integer('restaurant_number')-&gt;unsigned();
  $table-&gt;integer('transaction_id')-&gt;unsigned()-&gt;unique(); &lt;-- Right here
  </code></pre>
  
  <p>On <a href="https://laravel.com/docs/5.5/migrations#foreign-key-constraints" rel="nofollow noreferrer">docs</a> it says</p>
  
  <blockquote>
    <p>Laravel also provides support for creating foreign key constraints,
    which are used to force referential integrity at the database level.
    For example, let's define a user_id column on the posts table that
    references the id column on a users table:</p>
  </blockquote>
  
  <p>But on my <code>details</code> table I referencing the transaction_id from header which is not primary key and I got error this error <code>(errno: 150 "Foreign key constraint is incorrectly formed")</code>. How can reference it from a non-primary</p>
  
  <pre><code>$table-&gt;increments('id');
  $table-&gt;integer('restaurant_number')-&gt;unsigned();
  $table-&gt;integer('transaction_id')-&gt;unsigned()-&gt;nullable();
  $table-&gt;foreign('transaction_id')-&gt;references('transaction_id')-&gt;on('header');
  </code></pre>