-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem dropping foreign keys when run tests using in memory sqlite. #23461
Comments
This is not really a Laravel problem. It's more a sqlite configuration setting. Sqlite has no foreign key constrains enabled per default, so you'd have to enable it by yourself. You can enable those by executing following SQL after opening the db connection:
or you can tell Laravel to enable the setting for you with the enableForeignKeyConstraints() method of the Schema class:
Also you can have another look at the ordering of your migrations. Check if there are any "parent tables" that are being dropped before their "child tables" in reference of their indexes. If so, reorder your table dropping or change your indexes. I hope this helps. Best wishes |
just for reference Laravel 5.8
'connections' => [
'sqlite_testing' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => ':memory:',
'prefix' => '',
//set this env variable to false like this
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', false),
], or better in
|
This doesn't work for me for my Dusk tests. |
Did you find any work around, i'm updating my app from laravel 5.6 to 5.7 and on. All test are failing with an error |
Kind of. In my case I had the error when I was trying to drop a column during the public function down(): void
{
if (DB::getDriverName() !== 'sqlite') {
Schema::table('teams', function (Blueprint $table) {
$table->dropForeign(['venue_id']);
});
}
Schema::table('teams', function (Blueprint $table) {
$table->dropColumn('venue_id');
});
}` |
Facing the same issue on Laravel 6.x |
@troccoli I think you should remove the second drop column action, otherwise it will be execute even if we use SQLite: public function down(): void
{
if (DB::getDriverName() !== 'sqlite') { // Keep just this
Schema::table('teams', function (Blueprint $table) {
$table->dropForeign(['venue_id']);
});
}
}` |
No I shouldn't, that's the whole point of the I want to remove the So, if I'm using SQLite, i.e. during testing, I just remove the column (and SQLite is fine with that). If not, i.e. while using the app, I remove the foreign key first and then I can drop the column. Hope this helps. 😄 |
You are right :) Thank you for catching my misunderstanding. @troccoli |
I was running into the same issue with Laravel 7, I went with a similar approach to @troccoli. The main difference is I'm checking the environment value DB_CONNECTION, and the location of my IF statement.
|
I ended up adding this to my AppServiceProvider's boot method: Blueprint::macro('dropForeignSafe', function ($args) {
if (app()->runningUnitTests()) {
// Do nothing
/** @see Blueprint::ensureCommandsAreValid */
} else {
$this->dropForeign($args);
}
}); Then I replaced my calls to dropForeign with dropForeignSafe: $table->dropForeignSafe(['user_id']); |
Just fought with this issue for the past hour, and what I've noticed is that you should be using the |
@iSWORD Have you been unit testing this macro..?? |
If I drop the entire column, is then necessary also to remove the foreign key?
|
Another solution is to catch the exception : public function up() {
try {
Schema::table('table', function (Blueprint $table) {
$table->dropForeign('table_id_foreign');
});
} catch (Throwable) {
// some DB cannot drop foreign keys...
}
} |
Fixed on Laravel 11.15 via #51373 |
Description:
I have an old database that I did not write migrations for it. After a while I decided to write migration not only because I want to write unit tests, but also for development.
But my problems starts when I want to migrates from Laravel 5.2 to Laravel 5.3.
I want to update my jobs table according to the documentation for this purpose, but when I run tests again it produced an error :
`Testing started at 7:07 PM ...
/usr/bin/php7.0 /tmp/ide-phpunit.php --configuration /var/www/html/HomeLine/phpunit.xml
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
SQLSTATE[HY000]: General error: 1 no such index: jobs_queue_reserved_reserved_at_index (SQL: DROP INDEX jobs_queue_reserved_reserved_at_index)
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Connection.php:770
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Connection.php:726
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Connection.php:481
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:83
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:229
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:130
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237
/var/www/html/HomeLine/database/migrations/2018_03_09_183625_upgrate_job_table_to_laravel_53.php:20
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:373
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:380
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:162
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:130
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:97
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:65
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Container/Container.php:508
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Console/Command.php:169
/var/www/html/HomeLine/vendor/symfony/console/Command/Command.php:261
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Console/Command.php:155
/var/www/html/HomeLine/vendor/symfony/console/Application.php:817
/var/www/html/HomeLine/vendor/symfony/console/Application.php:185
/var/www/html/HomeLine/vendor/symfony/console/Application.php:116
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Console/Application.php:107
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:218
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:25
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:16
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:105
/var/www/html/HomeLine/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:70
/var/www/html/HomeLine/tests/TestCase.php:31
`
I should mention that everything is OK when I run the migrations with MySql database.
The text was updated successfully, but these errors were encountered: