Laravel how to create foreign key constrain from non-primary key

Viewed 1554

I have a table named header

$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->unique(); <-- Right here

On docs it says

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:

But on my details table I referencing the transaction_id from header which is not primary key and I got error this error (errno: 150 "Foreign key constraint is incorrectly formed"). How can reference it from a non-primary

$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->nullable();
$table->foreign('transaction_id')->references('transaction_id')->on('header');
1 Answers
Related