Allow SQLite to use foreign keys in Laravel 4

Because of backwards compatibility foreign key checking is by default disabled in SQLite databases. As current Laravel 4 version does not provide any elegant way of enabling that, a little hack is required to force foreign keys for SQLite connections.

Add the following code to the end of file app/start/global.php

if (DB::connection() instanceof Illuminate\Database\SQLiteConnection) {
  DB::statement(DB::raw('PRAGMA foreign_keys=1'));

Leave a Reply

Your email address will not be published. Required fields are marked *