我的HOOFDVRAAGS数据库中存在外键错误,但奇怪的是这与VRAAGS完全相同
SQLSTATE [HY000]:一般错误:1005无法创建表
zonetoets
.#sql-1 e8_2f9
(错误:150“外键约束形成错误”)(SQL:a lter tablehoofdvraags
add constrainthoofdvraags_toets_id_foreign
fore ign key(toets_id
)引用toets
(id
) )删除级联)在Connection.php第458行: SQLSTATE [HY000]:常规错误:1005无法创建表zonetoets
.#sql-1 e8_2f9
(错误号:150“外键约束形成错误”)
在此我的db-migrations,希望有人可以帮助我。因此关系TOETS 1 ----> N HOOFDVRAAGS 1 ----> N VRAAGS
主要问题
<?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');
}
}
BUTTON
<?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');
}
}
问题
<?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');
}
}
模型键
public function hoofdvraag()
{
return $this->hasMany(Hoofdvraag::class);
}
模型主要问题
public function subvraag()
{
return $this->hasMany(Subvraag::class);
}
public function toets() {
return $this->belongsTo(Toets::class);
}
模型子问题
public function hoofdvraag() {
return $this->belongsTo(Hoofdvraag::class);
}
据我所知,如果你在模型中创建一个雄辩的实现,为什么要在迁移中创建外键,只需要为它创建一个列。
https://laravel.com/docs/5.8/eloquent-relationships#one-to-many