通过迁移在Laravel 5.2中添加外键时出错

时间:2016-02-04 06:35:49

标签: sql migration laravel-5.2

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

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时,我收到以下错误:

[照亮\数据库\ QueryException]
  SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table jobs添加约束jobs_recruiter_id_foreign foreign
  密钥(recruiter_id)引用company_usersid))

[PDOException]
  SQLSTATE [HY000]:常规错误:1215无法添加外键约束

请帮忙。

1 个答案:

答案 0 :(得分:0)

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