我的HOOFDVRAAGS数据库中出现外键错误,但奇怪的是,这与VRAAGS中的完全相同
SQLSTATE [HY000]:常规错误:1005无法创建表
zonetoets
。#sql-1 e8_2f9
(错误号:150“外键约束为 格式不正确”)(SQL:过滤器表hoofdvraags
添加约束hoofdvraags_toets_id_foreign
个外键(toets_id
)引用toets
(删除级联上的id
)在Connection.php第458行:
SQLSTATE [HY000]:常规错误:1005无法创建表zonetoets
。#sql-1 e8_2f9
(错误号:150“外键约束为 格式不正确”)
这里是我的数据库迁移,希望有人可以帮助我。 特此关系 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');
}
}
模特脚
public function hoofdvraag()
{
return $this->hasMany(Hoofdvraag::class);
}
模型HOOFDVRAAG
public function subvraag()
{
return $this->hasMany(Subvraag::class);
}
public function toets() {
return $this->belongsTo(Toets::class);
}
模型SUBVRAAG
public function hoofdvraag() {
return $this->belongsTo(Hoofdvraag::class);
}
答案 0 :(得分:-1)
据我所知,如果要在模型中创建雄辩的房地产,为什么要在迁移中创建外键,则只需为其创建一列。
https://laravel.com/docs/5.8/eloquent-relationships#one-to-many