SQL:在Laravel中,修改表`familymembers`添加约束`familymembers_user_id_foreign`外键(`user_id`)引用`all_users`(`id`)

时间:2018-09-05 15:29:50

标签: php mysql laravel migration

我是laravel的新手。我试图将外键放在家庭成员表中。 但无法正确迁移。发生以下错误。我尝试了许多不同的更改来解决以下错误。但是我做不到。这不是重复的问题。请帮我...

  

在Connection.php第647行中:

     

SQLSTATE [HY000]:常规错误:1215无法添加外键   约束

(SQL: alter table `familymembers` add constraint `familymemb
  ers_user_id_foreign` foreign key (`user_id`) references `all_users` (`id`))
     

在Connection.php第449行中:

     

SQLSTATE [HY000]:常规错误:1215无法添加外键   约束

create_familymembers_table_migrations:

 Schema::create('familymembers', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
                $table->string('fullName');
                $table->string('relationship');
                $table->string('gender');
                $table->date('dob');

                $table->integer('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('all_users');

                $table->timestamps();

});

create_all_users_table_migrations:

Schema::create('all_users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nameWithInitials');
            $table->string('callingName');
            $table->string('email');
            $table->integer('contactNo');
            $table->string('password');
            $table->string('type');
            $table->string('addNo');
            $table->string('addStreet');
            $table->string('addCity');
            $table->string('intentToJoin');
            $table->string('region');

            $table->timestamps();

});

谢谢。

1 个答案:

答案 0 :(得分:1)

确保迁移文件的顺序正确。

例如all_users,然后familymembers