I am new to web development in Laravel 5.2 and I have this problem. I have a Users and Jobseekers tables and I am trying to create a foreign key in the Jobseekers table that references the Users table using migration but when I run php artisan migrate I get the error
我是Laravel 5.2的web开发新手,我有这个问题。我有一个用户和求职者表,我试图在求职者表中创建一个外键,它使用迁移引用用户表,但是当我运行php artisan迁移时,我得到了错误。
[Illuminate\Database\QueryException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key cons traint fails (jobsitelara
.#sql-1a04_7d
, CONSTRAINT jobseekers_user_id_foreign
FOREIGN KEY (user_id
) REFERENCES users
(id
)) (SQL: alter table jobseekers
add constraint jobseekers_user_id_foreign fore ign key (user_id
) references users
(id
))
[照亮数据库\QueryException] SQLSTATE[23000]:完整性约束违例:1452不能添加或更新一个子行:外键出错(jobsitelara)。# SQL -1a04_7d,约束jobseekers_user_id_foreign KEY (user_id)引用用户(id)
[PDOException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key cons traint fails (jobsitelara
.#sql-1a04_7d
, CONSTRAINT jobseekers_user_id_foreign
FOREIGN KEY (user_id
) REFERENCES users
(id
))
[PDOException] SQLSTATE[23000]:完整性约束违例:1452不能添加或更新子行:外键禁用失败(jobsitelara)。#sql-1a04_7d,约束jobseekers_user_id_foreign KEY (user_id)引用用户(id)
Here is the migration for create_users_table
这里是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();
});
and the migration for create_jobseekers_table
以及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();
});
}
and the migration to create the foreign key is in a separate file that runs after the create tables migrations have been run here is the add_foreignkey_to_jobseekers_table migration
创建外键的迁移是在创建表迁移之后运行的一个单独文件中,这里是add_foreignkey_to_jobseekers_table迁移
public function up()
{
Schema::table('jobseekers', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
I made sure that the user_id field in the Jobseekers table is also unsigned. I noticed that the tables are referenced as jobsitelara'.'#sql-1a04_8f' in the error messages, I don't understand what's going on. What else is wrong with my code?
我确保jobfinder表中的user_id字段也没有签名。我注意到这些表被引用为jobsitelara'。在错误信息中,我不知道发生了什么。我的代码还有什么问题吗?
2 个解决方案
#1
2
In Laravel Database Migrations, you need to have clear the order of your migrations and the parent – child relationship between your tables. Therefore, when you create tables, you need to create parent tables first and then child tables.
在Laravel数据库迁移中,您需要清楚迁移的顺序以及表之间的父-子关系。因此,在创建表时,需要先创建父表,然后创建子表。
In the Other hand, when you delete tables, you need to delete child tables first and then parents.
另一方面,在删除表时,需要先删除子表,然后删除父表。
@Martin Bean has an explanation on this topic What is a Parent table and a Child table in Database.
@Martin Bean对这个主题有一个解释,什么是数据库中的父表和子表。
When you run in your terminal php artisan migrate: --options--, your functions up and down will be called respectively depending on the option you pass to migrate. If you order your migrations having parent-child relationships clear, you won’t have this problem.
当您在终端php中运行artisan migration: -options时,根据您传递给migration的选项,将分别调用您的函数up和down。如果你命令你的迁移有亲子关系,你就不会有这个问题。
There are also other situations that you should consider when designing your database, like recursive relationships between tables which @Branko Dimitrijevic explains in this topic Database design for a recursive relationship.
在设计数据库时还应该考虑其他情况,比如表之间的递归关系,在本主题数据库设计中,@Branko Dimitrijevic解释了这种关系。
Hope it helps!
希望它可以帮助!
#2
0
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->timestamps();
});
}
public function up()
{
Schema::table('jobseekers', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
Remove user_id string from the create_jobseekers_table and put following foreign key table code on add_foreignkey_to_jobseekers_table
从create_jobseekers_table中删除user_id字符串,并在add_foreignkey_to_jobseekers_table上放置以下外键表代码
#1
2
In Laravel Database Migrations, you need to have clear the order of your migrations and the parent – child relationship between your tables. Therefore, when you create tables, you need to create parent tables first and then child tables.
在Laravel数据库迁移中,您需要清楚迁移的顺序以及表之间的父-子关系。因此,在创建表时,需要先创建父表,然后创建子表。
In the Other hand, when you delete tables, you need to delete child tables first and then parents.
另一方面,在删除表时,需要先删除子表,然后删除父表。
@Martin Bean has an explanation on this topic What is a Parent table and a Child table in Database.
@Martin Bean对这个主题有一个解释,什么是数据库中的父表和子表。
When you run in your terminal php artisan migrate: --options--, your functions up and down will be called respectively depending on the option you pass to migrate. If you order your migrations having parent-child relationships clear, you won’t have this problem.
当您在终端php中运行artisan migration: -options时,根据您传递给migration的选项,将分别调用您的函数up和down。如果你命令你的迁移有亲子关系,你就不会有这个问题。
There are also other situations that you should consider when designing your database, like recursive relationships between tables which @Branko Dimitrijevic explains in this topic Database design for a recursive relationship.
在设计数据库时还应该考虑其他情况,比如表之间的递归关系,在本主题数据库设计中,@Branko Dimitrijevic解释了这种关系。
Hope it helps!
希望它可以帮助!
#2
0
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->timestamps();
});
}
public function up()
{
Schema::table('jobseekers', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
Remove user_id string from the create_jobseekers_table and put following foreign key table code on add_foreignkey_to_jobseekers_table
从create_jobseekers_table中删除user_id字符串,并在add_foreignkey_to_jobseekers_table上放置以下外键表代码