January 27, 2017 • 1 minute read
Laravel Tests, SQLite and “no such column” errors
I ran into a frustrating issue today while trying to run PHPUnit tests on a Laravel project I’m working on. Essentially, I was getting PDOException
messages saying things like no such column. Looking through the stack trace, the issue seemed to have something to do with the Illuminate\Database\Console\Migrations\RollbackCommand
class, which suggested it may be an issue with a rollback migration.
After some digging around, I discovered that my rollback migrations where choking when running tests because I was dropping multiple columns at a time, but in separate statements, like as follows:
$table->dropColumn('read_time_minutes');
$table->dropColumn('word_count');
This works fine in my main MySQL database, but it was causing trouble with my testing SQLite database. To resolve the issue, you need to ensure that these operations happen within the same transaction. An easy way to do this is as follows:
$table->dropColumn(['read_time_minutes', 'word_count']);
And voilà, it works!