简体   繁体   中英

Laravel 5.1 - test database with SQLite gives 'no such index' error on migration down()

The following schema builder code works perfectly when running php artisan migrate and php artisan migrate:rollback on local and staging (production-like) environment. This will run an alter table statement to modify an existing table.

Note: since rows are empty, there is no need to set nullable or default value to the category_id :

// up
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

// down
$table->dropForeign('stores_category_id_foreign');
$table->dropColumn('category_id');

I'm running my functional tests with SQLite using :memory: configuration, and I'm getting the following error when the database rolls back (thanks to the DatabaseMigrations trait)

General error: 1 no such index: stores_category_id_index (SQL: DROP INDEX stores_category_id_index)

Why is this happening, is there something I have to configure on SQLite that I don't know of?

By default SQLite has foreign key support disabled.

You'll need to enabled it manually or use a different DB.

Laravel 5.1: Enable SQLite foreign key constraints

Link above talks about how to do this but essentially you need to find a way to run 'PRAGMA foreign_keys=1' on your functional test environment before tests.

Disclaimer: I've only tried this on Laravel 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM