Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 864
Christopher Francisco Avatar asked Aug 14 '15 01:08

Christopher Francisco


People also ask

How do I change the data type in laravel migration?

How to change data type of column in laravel 9 migration ? Step 1 : Install doctrine/dbal package. Step 2 : Generate migration file. Step 3 : Open generated migration file and update.

When can you get an SQLite schema error?

16) When can you get an SQLITE_SCHEMA error? The SQLITE_SCHEMA error is returned when a prepared SQL statement is not valid and cannot be executed. Such type occurs only when using the sqlite3 prepare() and sqlite3 step() interfaces to run SQL.

How many queries per second can SQLite handle?

Actually, SQLite will easily do 50,000 or more INSERT statements per second on an average desktop computer. But it will only do a few dozen transactions per second.


1 Answers

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

like image 55
kidshenlong Avatar answered Oct 06 '22 01:10

kidshenlong