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 !