-1

I was trying to make a foreign key in a primary key at one migration.

This is for Laravel 5.7. I have tried different ways to try to achieve my objective. This is my final code.

    Schema::create('teachers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('user_id')->unsigned()->primary();
        $table->timestamps();
    });

    Schema::table('teachers', function($table) {
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
    });

It makes a normal table with "user_id" as a primary key but it isn't a foreign key.

4 Answers4

0

Try this code

Schema::create('teachers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });
Vajiheh Habibi
  • 379
  • 1
  • 4
  • 16
  • @Joseph Whacheng Check this and let me know whether it was useful – Vajiheh Habibi Feb 01 '19 at 06:51
  • This works, but I don't want to have an extra "id", I want to the "user_id" to be the primary key of the table! – Joseph Whacheng Feb 01 '19 at 16:51
  • I do not know if these two links may help: https://stackoverflow.com/questions/48420676/laravel-use-primary-index-as-foreign-key https://stackoverflow.com/questions/41504219/laravel-schema-builder-primary-key-as-foreign-key – Vajiheh Habibi Feb 01 '19 at 20:26
0
Schema::create('teachers', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->integer('user_id')->unsigned()->nullable();
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
Lim Kean Phang
  • 501
  • 4
  • 6
0
<?php

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

class CreateTeachersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('teachers')) 
        {
            Schema::create('teachers', function (Blueprint $table) {
                $table->increments('id');
                $table->string('teacher_name');
                $table->unsignedInteger('user_id');
                $table->timestamps();
                $table->foreign('user_id')->references('id')->on('users');

            });
        }       
    }

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

Now You want to set the foriegn key of as id of users table in teachers table

AM I Clear

Now adding the column to techers table and adding foriegn key constarint

METHOD ONE

To Add the Column

$table->unsignedInteger('user_id');

To Add Primary Key

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Explaination now user_id is field name in teachers table references('id') is referencing the id on('users') is on user table

METHOD TWO

To Add the Column

$table->integer('user_id');

To Add Primary Key

$table->foreign('user_id')->references('id')->on('users')->unsigned()->onDelete('cascade');

Explaination now user_id is field name in teachers table references('id') is referencing the id on('users') is on user table ,unsigned() adding unsignedinteger to column

If Your issue is not solved kindly comment below

Hope it helps

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
0

Have you tried this?

Schema::create('teachers', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->integer('user_id')->unsigned();
    $table->timestamps();

    $table->primary('user_id');
    $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});

I don't think primary() can be chained onto the column definition the way unique() can.

Travis Britz
  • 5,094
  • 2
  • 20
  • 35