Skip to content

dropForeign method removes all my database tables with SQLite #52348

Closed
@iejfd930r2jfo32

Description

@iejfd930r2jfo32

Laravel Version

11.9

PHP Version

8.3.9

Database Driver & Version

SQLite 3.43.2 MacOS Sonoma Version 14.5 (23F79)

Description

Doing php artisan migrate:rollback removes all my tables in my database.

The down method in my migration file should drop the foreign key user_id. but instead it removes all my tables.

I use SQLite, and the Laravel documentation says :

SQLite only supports foreign keys upon creation of the table and not when tables are altered.

But #51373 PR should have made it possible to drop a foreign key with SQLite.

When writing the down method like that, I get an error which indicates (as mentionned in the docs) that SQLite cannot perfom the removal of a foreign key as it's the normal behavior :

public function down(): void
{
        Schema::table('employers', function (Blueprint $table) {
            $table->dropForeign('employers_user_id_foreign');
        });
}

BUT.... When writing my down method in the alternative way using an array to drop a foreign key, it removes everything. And when I say everything, it's really all my tables (except sqlite_sequences and migrations).

Steps To Reproduce

public function down(): void
{
        Schema::table('employers', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
        });
}

Here is what it does passing it in the dd() function :

   INFO  Rolling back migrations.

  2024_07_31_134927_add_user_fk_to_employers_table Illuminate\Support\Fluent^ {#1559
  #attributes: array:4 [
    "name" => "dropForeign"
    "index" => "employers_user_id_foreign"
    "columns" => array:1 [
      0 => "user_id"
    ]
    "algorithm" => null
  ]
} // database/migrations/2024_07_31_134927_add_user_fk_to_employers_table.php:26

And this removes all my database tables.

I also tried :

Schema::table('sessions', function (Blueprint $table) {
    $table->dropForeign(['user_id']);
    $table->dropColumn('user_id');
});

// And
Schema::table('sessions', function (Blueprint $table) {
    $table->dropConstrainedForeignId('user_id');
});

I get the same result, the removal of all my tables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions