dqwr32867 2016-05-29 09:21
浏览 32

Laravel迁移:在引用它之前创建引用的表

Here is my migration method:

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('item_type_id')->unsigned();
        $table->integer('character_class');
        $table->integer('character_race');
        $table->integer('required_level');
        $table->integer('quality');
        $table->integer('durability');
        $table->integer('buy_price');
        $table->integer('sell_price');
        $table->timestamps();
    });

    Schema::table('items', function($table) {
        $table->foreign('item_type_id')->references('id')->on('item_types')->onDelete('cascade');
    });
}

The problem is that migration for the item_types table is after items migration. So there is no item_types table while creating items table, then migration will fail at creating foreign key. Is there a way to delay foreign constraints and run them after table creations? Or I have to separate the foreign constraints to another migration?! Thanks.

  • 写回答

1条回答 默认 最新

  • douwen7331 2016-05-29 10:10
    关注

    There are two solutions:

    1

    Create item_types table before this migration (which is natural situation).

    2

    If the above is imposible for some reason then you have to move this:

    Schema::table('items', function($table) {
            $table->foreign('item_type_id')->references('id')->on('item_types')->onDelete('cascade');
        });
    

    To the end of up method in item_types table migration.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决