我是Laravel 5.2的Web开发新手,我遇到了这个问题。 我有一个用户和求职者表,我试图用迁移来建立在求职者表中引用的用户表的外键,但是当我运行 PHP工匠迁移我的错误Laravel 5.2迁移外键
[照亮\数据库\ QueryException ] SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键利弊 traint失败(jobsitelara
#sql-1a04_7d
,约束jobseekers_user_id_foreign
外键(user_id
)参考文献users
(id
))(SQL:改变表jobseekers
添加约束jobseekers_user_id_foreign fore ign key(user_id
)引用users
(id
))
[PDOException] SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键利弊 traint失败(jobsitelara
。 #sql-1a04_7d
,约束jobseekers_user_id_foreign
外键(user_id
)参考文献users
(id
))
这里是create_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
迁移和create_jobseekers_table
public function up()
{
Schema::create('jobseekers', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->enum('gender',array('male','female'));
$table->date('dateofbirth');
$table->string('occupation', 150);
$table->string('educationlevel', 200);
$table->string('cv', 150);
$table->string('skills', 200);
$table->string('address', 200);
$table->string('phonenumber', 30);
$table->integer('user_id');
$table->timestamps();
});
}
的迁移和迁移以创建外键位于创建表迁移后运行的单独文件中 这里是add_foreignkey_to_jobseekers_table迁移
public function up()
{
Schema::table('jobseekers', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
我确信,在求职者表user_ID的领域也无符号。 我注意到这些表被引用为jobsitelara'。'#sql-1a04_8f'在错误信息中,我不明白发生了什么事情。 我的代码还有什么问题?