【发布时间】:2019-02-10 19:35:03
【问题描述】:
我是 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();
});
提前致谢。
【问题讨论】:
-
迁移文件的顺序是什么? (按日期)。您是否创建了
all_users之后familymembers? -
哦..真的..我忘记了。没有原始表无法使用外键创建表。我对 laravel 的经验较少。我不考虑迁移日期。真的谢谢你非常喜欢。我浪费了很多时间来解决这个错误。
-
没关系!来帮忙。我已添加为答案,因此标记为正确。
标签: php mysql laravel migration