【问题标题】:Error adding a foreign key in Laravel 5.2 through migrations通过迁移在 Laravel 5.2 中添加外键时出错
【发布时间】:2016-05-13 15:44:32
【问题描述】:

我有两个迁移,如下所示:

2016_02_03_071404_create_company_users_table.php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCompanyUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('company_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies');
            $table->char('role', 20);
            $table->text('first_name');
            $table->text('last_name');
            $table->integer('age');
            $table->string('email')->unique();
            $table->text('password');
            $table->text('login_type');
            $table->string('phone_number')->unique();
            $table->integer('verified')->default(0);
            $table->text('profile_picture');
            $table->text('facebook_id');
            $table->text('twitter_id');
            $table->text('linkedIn_id');
            $table->text('google_plus_id');
            $table->text('current_location');
            $table->text('established_year');
            $table->text('device_type');
            $table->text('device_token');
            $table->text('device_model');
            $table->text('device_os_version');
            $table->text('last_login');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropifExists('company_users');
    }
}

2016_01_28_144808_create_jobs_table.php

<?php

use Carbon\Carbon;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('job_title');
            $table->text('job_description');
            $table->text('job_industry');
            $table->text('job_location');
            $table->integer('job_experience');
            $table->text('employment_type');
            $table->bigInteger('recruiter_id')->unsigned();
            $table->foreign('recruiter_id')->references('id')->on('company_users');
            $table->tinyInteger('status')->default(1);
            $table->timestamp('posted_date')->default(Carbon::now()->toDateTimeString());
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropifExists('jobs');
    }
}

当我运行php artisan migrate 时出现以下错误:

[照亮\数据库\查询异常]
SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQL: alter table jobs add constraint jobs_recruiter_id_foreign foreign
关键 (recruiter_id) 引用 company_users (id))

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

请帮忙。

【问题讨论】:

    标签: sql migration laravel-5.2


    【解决方案1】:

    我想通了...基本上是迁移文件的执行顺序。所以我更改了迁移文件的时间戳以重新排序执行......就像魅​​力一样。

    【讨论】:

      猜你喜欢
      • 2016-06-06
      • 2019-02-09
      • 2019-10-19
      • 2013-11-19
      • 1970-01-01
      • 2018-08-01
      • 2021-02-21
      • 2016-06-24
      • 2016-11-21
      相关资源
      最近更新 更多