通过迁移在 Laravel 5.2 中添加外键时出错
Error adding a foreign key in Laravel 5.2 through migrations
我有两个迁移,如下所示:
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
我得到以下错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQL: alter table jobs
add constraint jobs_recruiter_id_foreign foreign
键 (recruiter_id
) 引用 company_users
(id
))
[PDOException]
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束
请帮忙。
我想通了...这基本上是迁移文件的执行顺序。所以我更改了迁移文件的时间戳以重新排序执行....工作起来很有魅力。
我有两个迁移,如下所示:
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
我得到以下错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQL: alter table jobs
add constraint jobs_recruiter_id_foreign foreign
键 (recruiter_id
) 引用 company_users
(id
))
[PDOException]
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束
请帮忙。
我想通了...这基本上是迁移文件的执行顺序。所以我更改了迁移文件的时间戳以重新排序执行....工作起来很有魅力。