我正在使用Laravel6。我想运行迁移文件,但是在迁移“ create_users_table”文件时出现以下错误:
SQLSTATE[HY000]: General error: 1005 Can't create tab
le `thenewmeetingapp`.`#sql-f3c_b8` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table
`users` add constraint `users_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`))
我认为错误在于表“ users”和表“ permissions”之间(每个用户必须有一个许可,每个许可可以有许多用户)。 但是,表“用户”与表“ meeting_user”也相关,表“ meeting_user”是与表“ meetings”连接的表。
用户:
Schema::create('users',function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('username')->unique();
$table->string('password');
$table->bigInteger('permission_id')->unsigned();
$table->enum('is_active',array(0,1))->default(1);
$table->rememberToken();
$table->timestamps();
$table->foreign('permission_id')->references('id')->on('permissions');
});
权限:
Schema::create('permissions',function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
会议用户:
Schema::create('meeting_user',function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('meeting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('meeting_id')->references('id')->on('meetings')->onDelete('cascade');
});
用户表的迁移是要运行的第一个迁移。但是,我也尝试在权限表迁移之前和用户的权限表迁移之后运行,但没有任何变化。错误是相同的。 有人可以帮助我吗?