0

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan I am thrown the following error:

Modules migration table:

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateModulesTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('modules', function (Blueprint $table) {
            $table->id();
            $table->string('module_name');

            // foreign key

            $table->timestamps();
        });
    }

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

Lesson migration table:

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateLessonTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('lesson', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->integer('module_id')->unsigned();
            $table->text('content');
            $table->integer('created_by')->unsigned();
            $table->integer('updated_by');
            $table->string('enabled');
            $table->string('position');
            $table->timestamps();
        });

        Schema::table('lesson', function(Blueprint $table) {
            $table->foreign('module_id')->references('id')->on('modules');
        });
    }

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

Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create e.g. Users, Clients, Projects, Tasks, Statuses, Priorities, Types, Teams. Ideally, I want to create tables that hold this data with the foreign keys, i..e clients_project and project_tasks, etc.

Hope someone can help me to get started.

Mhoreen
  • 27
  • 7
  • In which migration you are getting error? `$table->unsignedBigInteger('module_id');` try this – Jayant Jun 14 '21 at 04:52
  • Does this answer your question? [Migration: Cannot add foreign key constraint](https://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint) – zahid hasan emon Jun 14 '21 at 05:14

2 Answers2

2

Add unsignedBigInteger for module_id column

Schema::create('lesson', function (Blueprint $table) {
  $table->id();
  $table->string('title');
  $table->unsignedBigInteger('module_id');
  $table->text('content');
  $table->integer('created_by')->unsigned();
  $table->integer('updated_by');
  $table->string('enabled');
  $table->string('position');
  $table->timestamps();
  $table->foreign('module_id')->references('id')->on('modules');
});
Biswajit Biswas
  • 859
  • 9
  • 20
mk23
  • 1,257
  • 1
  • 12
  • 25
1

Your foreign_key field and your id should be of the same type, for example, if modules.id is bigIncrements, your foreign_key inside your Lesson table also should be bigInteger.

Lesson Migration File

Schema::create('lesson', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->integer('module_id')->unsigned()->nullable();
    $table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
    $table->text('content');
    $table->integer('created_by')->unsigned();
    $table->integer('updated_by');
    $table->string('enabled');
    $table->string('position');
    $table->timestamps();
});

Note: You should make sure, your Modules table migration is running before Lesson table migration.

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38