Laravel:使用外键迁移

时间:2018-09-03 15:09:10

标签: mysql laravel

我知道这个问题已经问了很多时间了,但是我尝试了所有解决方案,但仍然给我一个错误。

我想迁移我的表:

class CreateFichesTable extends Migration
{

public function up()
{
    Schema::create('fiches', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('type');
        $table->string('nom');
        $table->string('description');
        $table->string('image');
        $table->integer('equipe_id')->unsigned();
        $table->timestamps();
    });

    Schema::table('fiches', function(Blueprint $table)
    {
        $table->foreign('equipe_id')->references('id')->on('equipes');
    });
}

}

class CreateEquipesTable extends Migration
{

public function up()
{
    Schema::create('equipes', function (Blueprint $table) {;
        $table->increments('id');
        $table->string('nom');
        $table->string('image');
        $table->timestamps();
    });
}

}

然后我得到:

Exception trace:

1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign 
key constraint")

我试图强制使用引擎“ InnoDB”,但仍然无法正常工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

创建新的迁移文件时,laravel会在2018_08_10_111004_这样的开始日期时间自动添加,以检测必须首先创建哪个迁移。如您在注释中所示,您的迁移文件是2018_08_31_141536_create_fiches_table.php2018_09_03_141649_create_equipes_table.php,您必须更改此迁移中的一个,以便首先将第二个迁移称为第一个迁移。
例如

2018_08_31_141536_create_fiches_table.php 
2018_08_30_141649_create_equipes_table.php
相关问题