Skip to content
Advertisement

Multiple primary keys found issue Laravel

here is the issue. I am banging my head for hours now on this. Laravel is not allowing me to have a primary key (it says multiple but you can see it has only one). And further more as there is issue causing in primary key in teachers table. I am not able to use this as a foreign key in courses table.

Migration file for teacher table

class CreateTeachersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('teachers', function (Blueprint $table) {
            $table->increments('teacher_id')->primary();
            $table->string('email', 200);
            $table->string('first_name',300)->nullable();
            $table->string('last_name',300)->nullable();
            $table->string('account_status',50)->default('inactive');
            $table->string('subscription_status',50);
            $table->string('account_reset_code',400)->nullable();
            $table->string('passkey',500);
            $table->timestamps();
        });
    }

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

Migration File for courses table

class CreateCoursesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('courses', function (Blueprint $table) {
            $table->increments('course_id')->primary();
            $table->integer('teacher_id_fr');
            $table->string('course_name', 500);
            $table->string('course_code',300);
            $table->string('subject_area',300);
            $table->string('course_level',100);;
            $table->string('grade_periods',100);
            $table->timestamps();    
        });

        Schema::table('courses',function(Blueprint $table){
            $table->foreign('teacher_id_fr')->references('teacher_id')->on('teachers')->onDelete('cascade')->onUpdate('cascade');

        });
    }

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

Issue raised on migrating

Syntax Error or access violation. 1068 Multiple primary keys found

.

What I’ve tried by now

1) Making the primary column unsigned using unsigned method.

2) using InnnoDB engine type for table.

3) And much other things as suggested for similiar posts on the forums but nothing seems to work for me.

EDIT

I’ve removed the primary() method from both files at $table->increments(‘teacher_id’). As this is said Laravel marks auto increments columns as primary keys.

But now the error is on courses table as it says.

General error : 1215 cannot add foreign key constraint

Advertisement

Answer

I was able to fix the issue with a minute change. I modified courses migration file like below and it worked like a charm

$table -> integer('teacher_id_fr') -> unsigned();

Reason

Actually both the referenced and referencing columns (teacher.teacher_id and course.teacher_id_fr in this case) must be of same type.

Having said that, Laravel marks autoincrement columns as “Primary” and of type unsigned. So make sure to change foregin key column to match the type of primary key i.e unsigned. which the above modification does.

Hope it clarifies the issue and solution. If you have anything else on your mind. Please give it a shot here.

Happy Learning!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement