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.

2
  • In which migration you are getting error? $table->unsignedBigInteger('module_id'); try this Commented Jun 14, 2021 at 4:52
  • Does this answer your question? Migration: Cannot add foreign key constraint Commented Jun 14, 2021 at 5:14

2 Answers 2

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');
});
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.