Laravel 5.2 迁移:无法添加 char 数据类型的外键

Kev*_*lds 5 php mysql laravel laravel-migrations

我正在尝试创建char 数据类型的可为空外键。当我运行迁移命令时。我收到以下错误。我不确定我哪里做错了。

[Illuminate\Database\QueryException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter tablelevels添加约束levels_sample_type_id_foreign外键 ( sample_type_id) 引用sample_types( id))

[PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束

这是级别表的迁移文件内容

public function up()
{
    Schema::create('levels', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->char('id', 36)->unique();
        $table->string('description')->nullable();
        $table->char('sample_type_id', 36)->nullable();
        $table->integer('order');
        $table->timestamps();
        $table->primary('id');
        $table->foreign('sample_type_id')->references('id')->on('sample_types');
    });
}
Run Code Online (Sandbox Code Playgroud)

对于sample_types表如下

public function up()
{
    Schema::create('sample_types', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->char('id', 36)->unique();
        $table->string('name');
        $table->timestamps();
        $table->primary('id');
    });
}
Run Code Online (Sandbox Code Playgroud)

mik*_*n32 3

如果所引用的表尚不存在,则外键语句将失败。确保在迁移文件sample_types中引用该表之前先创建该表levels