Bug: Database Migrations - Problem in resolving constrained table name #53149
-
Laravel Version11.19.0 PHP Version8.3.4 Database Driver & VersionSQLITE Descriptionwhen I was trying to insert into categories table i get this error INFO Seeding database.
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1 no such table: main.parent_categories (Connection: sqlite, SQL: insert into "categories" ("name", "slug") values (tech, tech)) the solution was to specify manually constrained table name like this $t->foreignIdFor(Category::class, 'parent_category_id')->nullable()->constrained('categories')->onDelete('cascade'); categories migration <?php
use App\Models\Category;
use App\Models\Media;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $t) {
$t->id();
$t->string('name');
// before fix
$t->foreignIdFor(Category::class, 'parent_category_id')->nullable()->onDelete('cascade');
// afted fix
$t->foreignIdFor(Category::class, 'parent_category_id')->nullable()->constrained('categories')->onDelete('cascade');
$t->string('slug');
$t->foreignIdFor(Media::class)->nullable();
$t->string('description')->nullable();
$t->string('status')->default('published');
$t->timestamps();
$t->unique(['parent_category_id', 'slug']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
}; Steps To ReproduceCreate this migration and try to insert into categories <?php
use App\Models\Category;
use App\Models\Media;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('categories', function (Blueprint $t) {
$t->id();
$t->string('name');
$t->foreignIdFor(Category::class, 'parent_category_id')->nullable()->onDelete('cascade');
$t->string('slug');
$t->foreignIdFor(Media::class)->nullable();
$t->string('description')->nullable();
$t->string('status')->default('published');
$t->timestamps();
$t->unique(['parent_category_id', 'slug']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('categories');
}
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What is the bug? The schema builder does not keep a copy of the model instance. So when calling As you are using a custom foreign key name (due to a self-referencing foreign key), it correctly tries to guess from that name. From the docs:
Reference: https://laravel.com/docs/11.x/migrations#foreign-key-constraints When using a custom foreign key, just provide the table name to the Also, why not just using |
Beta Was this translation helpful? Give feedback.
What is the bug?
The schema builder does not keep a copy of the model instance. So when calling
constrained()
it tries to guess the table from the foreign key name.As you are using a custom foreign key name (due to a self-referencing foreign key), it correctly tries to guess from that name.
From the docs:
Reference: https://laravel.com/docs/11.x/migrations#foreign-key-constraints
When using a custom foreign key, just provide the table name to the
constrained()
method.Als…