doulei3488 2017-04-06 19:22
浏览 265

使用Laravel迁移处理外键的正确方法?

I'm a newbie to laravel and migrations, and I can't seem to figure out the correct way to set up a foreign key.

Schema::create('sights', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->integer('contact_id');
        $table->integer('media_id');
        $table->timestamps();
    });

    Schema::table('sights', function($table) {
        $table->foreign('contact_id')->references('id')->on('contacts');
        $table->foreign('media_id')->references('sight_id')->on('sightMedia');
    });

If it's not clear, "contact_id" is a foreign key that's supposed to reference the column "id" in the table called "contacts", and "media_id" references the column "sight_id" in the intermediary table "sightMedia".

The error it gives me when I try to migrate is the following:

error screenshot

EDIT: This is the users table, it's second in the line of migrations so I suppose that's where the migrations stop and report an error.

        Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email');
        $table->string('firstName');
        $table->string('lastName');
        $table->string('password');
        $table->string('facebook');
        $table->integer('score_id')->unsigned();
        $table->integer('role_id')->unsigned();
        $table->timestamps();
    });

    Schema::table('users', function ($table) {
        $table->foreign('score_id')->references('id')->on('scores');
        $table->foreign('role_id')->references('id')->on('roles');
    });

Thank you in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 r语言神经网络自变量重要性分析
    • ¥15 基于双目测规则物体尺寸
    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢