首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel :带外键的迁移

Laravel :带外键的迁移
EN

Stack Overflow用户
提问于 2018-09-03 15:09:10
回答 1查看 49关注 0票数 1

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

我想迁移我的桌子:

代码语言:javascript
复制
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');
    });
}

}

代码语言:javascript
复制
class CreateEquipesTable extends Migration
{

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

}

我得到了:

代码语言:javascript
复制
Exception trace:

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

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

有小费吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-03 15:36:15

当您创建新的迁移文件时,laravel会自动添加到开始日期时间中,比如这个2018_08_10_111004_,用于检测必须首先创建哪个迁移。正如您在注释中显示的,您的迁移文件是2018_08_31_141536_create_fiches_table.php2018_09_03_141649_create_equipes_table.php,您必须将其中一个迁移更改为首先称为第二个迁移,第一个迁移。

例如

代码语言:javascript
复制
2018_08_31_141536_create_fiches_table.php 
2018_08_30_141649_create_equipes_table.php
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52152309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档