简体   繁体   中英

Laravel Table Migration: Cannot add foreign key constraint

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 eg 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.

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');
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM