Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

khaled-al-shweike's avatar

SQLSTATE[42S01]:

Hi there , iam working on laravel project for an app , while i've done working with the migrations of the DB , but when i run pgp artisan migrate this error shows up and i didn't know what is the solutiin or what cause of this error, the error : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'teams' already exists (Connection: mysql, SQL: create table teams (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, sport_type_id bigint unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

the DB that have the error :


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('teams', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedBigInteger('sport_type_id');
            $table->foreign('sport_type_id')->constraint()->onDelete('cascade')->onUpdate('cascade');
            $table->timestamps();
        });
    }
    public function down(): void {
        Schema::dropIfExists('teams');  }
}; ```


the db that have the foreignkey that i have called in the table above :


```  <?php

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('sport_types', function (Blueprint $table) {
            $table->id();
            $table->string('sport_type');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sport_types');
    }
};  ```
0 likes
7 replies
tykus's avatar

Base table or view already exists: 1050 Table 'teams' already exists

The error message explains the problem clearly. If this is your development environment and you are not worried about the existing data (why would you be...), then you can drop all tables and re-run all of the migrations using:

php artisan migrate:fresh

or, if you have seeders

php artisan migrate:fresh --seed
khaled-al-shweike's avatar

@tykus i am actaully doing this and the problem is not like what the error meassge are saying , iam calling the php artisan migrate:fresh and nothing changes

tykus's avatar

@khaled-al-shweike yet you have only shared that particular error message???

khaled-al-shweike's avatar

@tykus absolutely not , ishared the error messages with the tables but they are not showing up on the post don't know why , BTW this is the table with the error : Schema::create('teams', function (Blueprint $table) { $table->id(); $table->string('name'); $table->BigInteger('sport_type_id')->unsigned(); $table->foreign('sport_type_id')->constraint(); $table->timestamps(); });

and this the table where the foreignkey are located : public function up(): void { Schema::create('sport_types', function (Blueprint $table) { $table->bigInteger('id')->unsigned()->primary(); $table->string('sport_type'); $table->timestamps(); }); }

kokoshneta's avatar
Level 27

@khaled-al-shweike To show code in code blocks, put three backticks (```) on a separate line before and after the code. If you paste HTML without wrapping it in code blocks, it will get stripped out.

1 like
tykus's avatar

@khaled-al-shweike so, what have you done; made a second migration that attempts to create a teams table???

Please or to participate in this conversation.