我是 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/