tohid
tohid

Reputation: 51

SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_work_faranesh`.`learnings`

hello i use this migrate and see this error

Schema::create('learnings', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('course_id')->unsigned();
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });

error:

 SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_work_faranesh`.`learnings` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tab
le `learnings` add constraint `learnings_course_id_foreign` foreign key (`course_id`) references `courses` (`id`) on delete cascade)

and course migrate

Schema::create('courses', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade');   
    });

Upvotes: 0

Views: 69

Answers (2)

bjeftic
bjeftic

Reputation: 66

Try to create migration like this:

First create Courses, and after that Learnings.

Schema::create('courses', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade');   
        });


Schema::create('learnings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('course_id')->unsigned();
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });

Because you use unsigned twice for one column.

Upvotes: 1

AndreyBlog
AndreyBlog

Reputation: 9

You can set foreign keys only in tables that already exist. Try this:

    Schema::create('courses', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id')->unsigned();
    });
    Schema::table('courses', function (Blueprint $table) {
        $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade');
    });

And:

        Schema::create('learnings', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('course_id')->unsigned();
            $table->bigInteger('user_id')->unsigned();
            $table->timestamps();
        });
        Schema::table('learnings', function (Blueprint $table) {
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });

Upvotes: 1

Related Questions