Roemer
Roemer

Reputation: 1386

Laravel migration can't add foreign key

I'm trying to write a laravel database migration but I'm getting the following error about a foreign key:

  [Illuminate\Database\QueryException]                                                                                                                                                                                                                    
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'category_id' doesn't exist in table (SQL: alter table `subcategories` add constraint subcategories_category_id_foreign foreign key (`category_id`) references `categories` (`id`))  



  [PDOException]                                                                                           
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'category_id' doesn't exist in table 

The categories and subcategories tables do get created but the foreign key doesn't. Here's my migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function ($table) {
            $table->increments('id')->unsigned();
            $table->string('name')->unique();
        });

        Schema::create('subcategories', function ($table) {
            $table->increments('id')->unsigned();
            $table->foreign('category_id')->references('id')->on('categories');
            $table->string('name')->unique();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('categories');
        Schema::drop('subcategories');
    }
}

Any ideas? Thanks!

Upvotes: 32

Views: 34073

Answers (5)

Denis Lopatin
Denis Lopatin

Reputation: 65

Just use $table->foreignId('category_id')->references('id')->on('categories');

Upvotes: -1

Arash Younesi
Arash Younesi

Reputation: 1760

Laravel 7, 8

As Limon Monte mentioned firstly create column then add foreign key constraints

$table->foreignId('category_id');
$table->foreign('category_id')->references('id')->on('categories'); 
        

Upvotes: 4

Princewill Iroka
Princewill Iroka

Reputation: 546

Integer didn't work for me. Rather, I used bigInteger to create foreign key:

$table->bigInteger('category_id')->unsigned()->index()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

Upvotes: 2

Roemer
Roemer

Reputation: 1386

I forgot to add ->get() to the methods I called.

Upvotes: 0

Limon Monte
Limon Monte

Reputation: 54439

You should create column before creating a foreign key:

$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');

Documentation: http://laravel.com/docs/5.1/migrations#foreign-key-constraints

Upvotes: 85

Related Questions