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 tablehoofdvraags
add constrainthoofdvraags_toets_id_foreign
fore ign key (toets_id
) referencestoets
(id
) on delete cascade) In Connection.php line 458:
SQLSTATE[HY000]: General error: 1005 Can't create tablezonetoets
.#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);
}
toets
, thenhoofdvraags
thensubvraags
. Also you can use normal pluralization for the table names, and just define theprotected $table = 'hoodvragen';
in your model – Flame