【发布时间】:2020-01-15 04:35:00
【问题描述】:
我正在关注 Laracasts TDD,但遇到了一个无论我做什么都无法解决的错误。当我尝试迁移时,它向我显示此错误:
Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1005 无法创建表
birdboard.#sql-14b8_86(errno: 150 “外键约束的格式不正确”)(SQL:alter tableprojects添加约束projects_owner_id_foreign外键 (owner_id) 引用users(id))
我知道 Laravel 现在使用 unsignedBigInteger 代替 unsignedInteger 和 bigIncrements 代替 Increments。我已经完成了所有这些。 我也添加到我的 AppServiceProvider.php 中:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
应用服务提供商:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
Schema::defaultStringLength(191);
}
}
创建项目表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProjectsTable extends Migration
{
public function up()
{
Schema::create('projects', function (Blueprint $table) {
Schema::dropIfExists('projects');
$table->bigIncrements('id');
$table->unsignedBigInteger('owner_id');
$table->string('title');
$table->text('description');
$table->timestamps();
$table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('projects');
}
}
您能告诉我我的代码有什么问题吗?为什么会出现错误? https://prnt.sc/p5ku4x
【问题讨论】:
-
从 create
Schema::dropIfExists('projects');中删除这一行。问题似乎出在鸟板桌而不是这个? -
什么版本的 Laravel?
-
是
id的users也是bigIncrement吗? -
尝试运行
migrate:fresh -
@DinoNumić
birdboard好像是数据库名,不是表名