skip to Main Content

I am trying to link 2 tables in a laravel migration. But I keep getting the General error: 1215. And I don’t know why.

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->bigIncrements('videoNr');
            $table->char('filenaam');
            $table->timestamp('upload_datum')->useCurrent();
            $table->integer('oefNr')->foreign();
            $table->foreign('videoNr')->references('videoNr')->on('videoNr');

        });
    }

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

This is the code of the one I am trying to link to the other one.

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('exercise', function (Blueprint $table) {
            $table->bigIncrements('oefNr');
            $table->char('oefening_naam');
            $table->char('uitleg');
            $table->integer('videoNr')->foreign();
            $table->char('accountNr');
            
        });
    }

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

This is the code for the one I am trying to link to.

I have also tried changing where the key is. So I put it in exercise instead of videos. But the problem with that is that it first creates the exercise table and then the I get a different error. Because it tries to link to a table that doesn’t exists yet.

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ‘videoNr’ (Connection: mysql, SQL: create table videos (videoNr bigint unsigned not null auto_increment primary key, filenaam char(255) not null, upload_datum timestamp not null default CURRENT_TIMESTAMP, oefNr int not null, videoNr bigint unsigned not null) default character set utf8mb4 collate ‘utf8mb4_unicode_ci’). This is the first error code.

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Connection: mysql, SQL: alter table videos add constraint videos_videonr_foreign foreign key (videoNr) references videoNr (videoNr)) I have also encounterd this error message.

2

Answers


  1. Chosen as BEST ANSWER

    I have just found the awnser. This code works. I changed the where the foreign key was. And also changed my foreign to foreignId. And to change the order they migrate you can change the filename. Because they save the date as yyyymmddhhmmss, you can just put the make it so its created earlier.The first code, so exercise table.

    This is the second code, the video table.

    Sorry I couldn't paste my code in here for some reason.


  2. Note: remove all the bigIntegers that you’re assigning foreign keys and use this:
    Assuming that your relations are like -> An exercise has many videos.

    In your parent table exercise:

    $table->id();//add primary keys
    

    in your child table videos:

    $table->foreignId('exercise_id')->constrained();
    

    if you have model then for adding foreign keys:

    $table->foreignIdFor(AppModelsExercise::class);
    

    For using your method the current one make foreign relations like this foreign keys are bigIncrements and under the hood unsignedBigInteger :

    $table->unsignedBigInteger('exercise_id');
    $table->foreign('exercise_id')->references('id')->on('exercise');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search