0
votes

I've foreign key error in my HOOFDVRAAGS database but the strange this is that's the exact same as in VRAAGS

SQLSTATE[HY000]: General error: 1005 Can't create table zonetoets.#sql-1 e8_2f9 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: a lter table hoofdvraags add constraint hoofdvraags_toets_id_foreign fore ign key (toets_id) references toets (id) on delete cascade) In Connection.php line 458:
SQLSTATE[HY000]: General error: 1005 Can't create table zonetoets.#sql-1 e8_2f9 (errno: 150 "Foreign key constraint is incorrectly formed")

Hereby my db-migrations and hopefully someone can help me. Hereby the relationships TOETS 1---->N HOOFDVRAAGS 1 ----> N VRAAGS

HOOFDVRAAGS

<?php

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

class CreateHoofdvraagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('hoofdvraags', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('toets_id')->unsigned();
            $table->string('titel');
            $table->string('tekst');
            $table->string('bestandsnaam');
            $table->integer('volgnummerHoofdvraag')->default(99);             
            $table->timestamps();

            $table->foreign('toets_id')
                -> references('id')
                -> on('toets')
                -> onDelete('cascade');
        });
    }

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

TOETS

<?php

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

class CreateToetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('toets', function (Blueprint $table) {
            $table->increments('id');
            $table->string('toetscode');
            $table->string('jaargang');
            $table->string('vak')->default('wiskunde');
            $table->string('hoofdstuk');
            $table->string('hoofdstuktitel');
            $table->string('maker');
            $table->string('datumgemaakt');   
            $table->integer('volgnummerToets')->default(1);     
            $table->timestamps();
        });
    }

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

VRAAG

<?php

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

class CreateSubvraagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('subvraags', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('hoofdvraag_id')->unsigned();
            $table->string('vraag');
            $table->string('antwoord');
            $table->integer('punten');
            $table->string('bestandsnaam');
            $table->integer('volgnummerSubvraag')->default(1);
            $table->timestamps();

            $table->foreign('hoofdvraag_id')
                -> references('id')
                -> on('hoofdvraags')
                -> onDelete('cascade');
        });
    }

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

MODEL TOETS

public function hoofdvraag()
    {
        return $this->hasMany(Hoofdvraag::class);
    }

MODEL HOOFDVRAAG

public function subvraag()
    {
        return $this->hasMany(Subvraag::class);
    }

    public function toets() {
        return $this->belongsTo(Toets::class);
    }

MODEL SUBVRAAG

public function hoofdvraag() {
        return $this->belongsTo(Hoofdvraag::class);
    }
1
Can this be the order of execution? You need to make sure to create toets , then hoofdvraags then subvraags. Also you can use normal pluralization for the table names, and just define the protected $table = 'hoodvragen'; in your modelFlame
Flame, thanks I've changed the order of execution and now I don't have any error, great thanks !Peter

1 Answers

-1
votes

as i know , if you are creating an eloquent realtionship inside the models why you are creating the foreign key inside the migration all you need is create a column for it.

https://laravel.com/docs/5.8/eloquent-relationships#one-to-many