Reputation: 1185
I am following Laracasts TDD and i ran into an error that i cannot resolve no matter what i do. When i try to migrate it shows me this error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
birdboard
.#sql-14b8_86
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableprojects
add constraintprojects_owner_id_foreign
foreign key (owner_id
) referencesusers
(id
))
I know Laravel now uses unsignedBigInteger instead of unsignedInteger and bigIncrements instead of Increments. I've done all of that. Also into my AppServiceProvider.php i have added:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
App Service Provider:
<?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);
}
}
Create Projects table
<?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');
}
}
Can you please tell me whats wrong on my code and why do i get the error? https://prnt.sc/p5ku4x
Upvotes: 0
Views: 103
Reputation: 782
I think you need to edit the question and post your owner model too. there may be a difference in datatype.
$table->unsignedBigInteger('owner_id');
verify if you are using the same data type in both tables for owern_id in foreign and id in the primary table
Upvotes: 1