Description
- Laravel Version: Laravel Framework 9.23.0
- PHP Version: 8.1.9 (cli)
- Database Driver & Version: 8.0.29 MySQL Community Server - GPL
Description:
$table->dropForeignIdFor($model, $column) in migration attempts (and obviously fails) to drop wrong column name even though the column name (second parameter) is provided.
The issue seems to be with laravel extending the column name on its own going against the behaviour that I would expect.
Namely just dropping the provided column name if a column name is provided (and if not a column that follows laravel naming conventions for the referenced model)
And laravel only produces this unexpected behaviour with the dropForeingIdFor method. The analogue foreignIdFor method with the same arguments creates a column with the correct (provided) column name without adding anything to it.
The issue should be neither with php nor the database.
Steps To Reproduce:
- Set up php, Laravel with Sail (including mysql), the db-connection etc.
- Add foreignId in the up() method of a migration using the foreignIdFor($model, $column) method and provide both the model as well as the column name like:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignIdFor(UserGlobalRole::class, 'global_role_id');
});
}
- Add the corresponding dropForeignIdFor($model, $column) to the down method of the same migration with the same model and column as in the up-method like:
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeignIdFor(UserGlobalRole::class, 'global_role_id');
});
}
- run migration in console (I simply used: sail artisan migrate)
- replacing sail with php should have the same effect
- if db-connection is correctly set up database shows the column was correctly created under the give column-name 'global_role_id'
- roll back last migration in console (I simply used: sail artisan migrate:rollback)
- again: replacing sail with php should have the same effect if the db connection is set up
- attempts to drop column 'users_global_role_id_foreign' even tho I handed over the column name 'global_role_id' and it throws error in command line like:
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'users_global_role_id_foreign'; check that column/key exists (SQL: alter table users
drop foreign key users_global_role_id_foreign
)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
- changing the column name in step 2 or the table name (to another already existing table) results in the same error but attempting to drop a changed column following the pattern:
- {table}_{column}_foreign while it should just be column
- if no column is provided (or null), column will simply default to laravel naming conventions but still following the same pattern:
-> {table}_{model_name}_id_foreign while this as well should only be {model_name}_id