loddaa's avatar
Level 3

SQLite : dropForeign method removes all my database tables after doing migrate:rollback

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.

When writing my 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 :

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

The error :

 INFO  Rolling back migrations.

  2024_07_31_134927_add_user_fk_to_employers_table ..................................................................................... 2.92ms FAIL

   RuntimeException

  This database driver does not support dropping foreign keys by name.

  at vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php:526
    522▕      */
    523▕     public function compileDropForeign(Blueprint $blueprint, Fluent $command)
    524▕     {
    525▕         if (empty($command->columns)) {
  ➜ 526▕             throw new RuntimeException('This database driver does not support dropping foreign keys by name.');
    527▕         }
    528▕
    529▕         // Handled on table alteration...
    530▕     }

BUT... When writing my down function like this (which is the alternative way to drop a foreign key), it removes everything. And when I say everything, it's really all my tables (except sqlite_sequences and migrations) :

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

Here is what it does :

   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

So my question is why? Why don't I get an error like the previous code as this code should do exactly the same as the previous code?

And why does it removes all my tables?

And also, how to write a proper downmethod when trying to remove a foreign key using SQLite ?

Thank you all for your help !

0 likes
1 reply
loddaa's avatar
loddaa
OP
Best Answer
Level 3

After opening a Github issue, I received an answer to my issue :

Answer :

The dropForeign for SQLite is available since Laravel 11.15+ you have Laravel 11.9. Besides that, migrate:rollback rolls backs the last "batch" of executed migrations, but it doesn't call down method of recently added (not executed?) migration as you expect.

How I resolved my issue :

Hi @hafezdivandari ! Thank you for enlightening me about the rollback command 👍. I naively assumed that php artisan migrate:rollback would roll back the last migration file. But indeed, as you mentioned, it rolled back the last migration batch.

The issue is that I did php artisan migrate:fresh followed by php artisan migrate:rollback, which rolled back all the migration files (tell me if I'm wrong).

To roll back the last migration file, I did php artisan migrate:rollback --step=1, which worked as expected thanks to your contribution in your pull request.

Normally, as I mentionned, SQLite does not support to remove a foreign key but thanks to this Pull Request it does now.

For more info, see the issue on Github

1 like

Please or to participate in this conversation.