Laravel 5.1 -使用SQLite测试数据库给出'迁移down()时出错


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

当在本地暂存(类生产)环境上运行php artisan migratephp artisan migrate:rollback时,以下模式构建器代码可以完美地工作。这将运行一个alter table语句来修改一个已存在的表。

注意:由于行为空,不需要将nullabledefault value设置为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');

我正在运行我的功能测试与SQLite使用:memory:配置,我得到以下错误时,数据库回滚(多亏了DatabaseMigrations特性)

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

为什么会发生这种情况,是否有一些我必须在SQLite上配置,我不知道?

默认情况下,SQLite不支持外键。

您需要手动启用它或使用不同的DB。

Laravel 5.1:启用SQLite外键约束

上面的链接讨论了如何做到这一点,但本质上你需要找到一种方法在测试之前在你的功能测试环境中运行'PRAGMA foreign_keys=1'。

免责声明:我只在Laravel 5上试过