<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('pizza_product', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('pizza_id');
$table->unsignedBigInteger('product_id');
$table->timestamps();
// Define foreign keys with onDelete('cascade')
$table->foreign('pizza_id')->references('id')->on('pizzas')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('pizza_product');
}
};
The error :
Illuminate\Database\QueryException
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'pizza_product' already exists (Connection: mysql, SQL: create table `pizza_product` (`id` bigint unsigned not null auto_increment primary key, `pizza_id` bigint unsigned not null, `product_id` bigint unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at vendor\laravel\framework\src\Illuminate\Database\Connection.php:822
818▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
819▕ );
820▕ }
821▕
➜ 822▕ throw new QueryException(
823▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
824▕ );
825▕ }
826▕ }
1 vendor\laravel\framework\src\Illuminate\Database\Connection.php:580
PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'pizza_product' already exists")
2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:580
PDOStatement::execute()
#Migration error
13 messages · Page 1 of 1 (latest)
Base table or view already exists: 1050 Table 'pizza_product' already exists
the table already exists, guess you have run the migration twice?
i ran with
php artisan migrate:refresh
within the complete project it is mentioned twice and it's the same migration
that's weird. as the command should drop all the tables and migrate them again.
that's normal, 1 for creating, 1 for dropping the table when you rollback the migration
Yes
can you try again, and share the logs generated at migration?
just a sanity check, this is local development right? since this will drop all the existing tables