2016-02-24 167 views
1

我是Laravel 5.2的Web开发新手,我遇到了这个问题。 我有一个用户和求职者表,我试图用迁移来建立在求职者表中引用的用户表的外键,但是当我运行 PHP工匠迁移我的错误Laravel 5.2迁移外键

[照亮\数据库\ QueryException ] SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键利弊 traint失败(jobsitelara#sql-1a04_7d,约束jobseekers_user_id_foreign外键(user_id )参考文献usersid))(SQL:改变表jobseekers添加约束jobseekers_user_id_foreign fore ign key(user_id)引用usersid))

[PDOException] SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键利弊 traint失败(jobsitelara#sql-1a04_7d,约束jobseekers_user_id_foreign外键(user_id )参考文献usersid))

这里是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'在错误信息中,我不明白发生了什么事情。 我的代码还有什么问题?

回答

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'); 
    }); 
} 

从create_jobseekers_table移除USER_ID字符串,并把下面就add_foreignkey_to_jobseekers_table

2

外键表的代码在Laravel数据库迁移,你需要有明确的迁移过程的顺序和父 - 子你的表格之间的关系。因此,当您创建表格时,需要先创建父表格,然后使用,然后使用子表格。

在另一方面,当你删除表,您需要删除子表第一然后父母。

@Martin Bean对此主题有说明What is a Parent table and a Child table in Database

当您在终端PHP工匠运行迁移--options--,你的功能下来将分别取决于你传递给迁移选项调用。如果您订购了具有亲子关系的迁移,您将不会遇到此问题。

在设计数据库时还应考虑其他情况,例如@Branko Dimitrijevic在本主题Database design for a recursive relationship中解释的表之间的递归关系。

希望它有帮助!