Laravel 5.2迁移外键

时间:2016-02-24 12:42:35

标签: php mysql database laravel-5.2

我是Laravel 5.2中的Web开发新手,我遇到了这个问题。 我有一个Users和Jobseekers表,我试图在Jobseekers表中创建一个外键,该表使用迁移引用Users表但是当我运行时 php artisan migrate我收到错误

[照亮\数据库\ QueryException]   SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键缺点   traint失败(jobsitelara#sql-1a04_7d,CONSTRAINT jobseekers_user_id_foreign FOREIGN KEY(user_id   )REFERENCES usersid))(SQL:alter table jobseekers添加约束jobseekers_user_id_foreign fore   ign键(user_id)引用usersid))

[PDOException]   SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键缺点   traint失败(jobsitelara#sql-1a04_7d,CONSTRAINT jobseekers_user_id_foreign FOREIGN KEY(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');
    });
}

我确保Jobseekers表中的user_id字段也是无符号的。 我注意到这些表在错误消息中被引用为 jobsitelara'#sql-1a04_8f' ,我不明白发生了什么。 我的代码还有什么问题?

2 个答案:

答案 0 :(得分:2)

Laravel数据库迁移中,您需要清除迁移的顺序以及表之间的父 - 子关系。因此,在创建表时,需要创建父表第一个然后子表。

另一方面,当您删除表格时,您需要删除子表第一个然后父项。

@Martin Bean对此主题有What is a Parent table and a Child table in Database的解释。

当您在终端 php artisan migrate - 选项 - 中运行时,您的功能向上向下将分别根据您传递的选项进行调用。如果您订购具有父子关系的迁移,则不会出现此问题。

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

希望它有所帮助!

答案 1 :(得分: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上

相关问题