php - SQL : alter table `familymembers` add constraint `familymembers_user_id_foreign` foreign key (`user_id` ) references `all_users` (`id` ) in laravel

标签 php mysql laravel migration

我是 Laravel 的新手。我试图将外键放入 familymembers 表中。 但无法正确迁移。出现以下错误。我尝试了很多不同的更改来解决以下错误。但我做不到。这不是重复的问题。请帮助我...

In Connection.php line 647:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

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

In Connection.php line 449:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

创建家庭成员表_迁移:

 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();

});

创建_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();

});

提前致谢。

最佳答案

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

例如all_users,然后是familymembers

关于php - SQL : alter table `familymembers` add constraint `familymembers_user_id_foreign` foreign key (`user_id` ) references `all_users` (`id` ) in laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52188803/

相关文章:

PHP - 分页 - 为什么我收到此错误 : not found in MySQL result index 14?

javascript - JsTree 和 Laravel 的麻烦

laravel - 如何在laravel的命名路由中传递get参数

php - 如果通过 laravel5 pluck(lists) 方法创建,如何将字符串添加到数组键?

php - 降低验证码 zend 表单元素的难度级别?

php - 匹配正则表达式后如何获取文本

PHP Excel 导入到 MYSQL 数据库在本地主机上工作正常,但在在线服务器上不行

php - 如何连接到 SQLYog 本地主机或其他方式

php - Woocommerce(wordpress) 和 opencart 集成

mysql - 多业务客户管理数据库设计