DBIx::Class How do I enable PRAGMA foreign_keys for SQLite?

dbix-class, perl, sqlite

Solution

Both

my $schema = MyApp::Schema->connect(
    'dbi:SQLite:db/myapp.db',
    undef,
    undef,
    {
        on_connect_do => 'PRAGMA foreign_keys = ON',
    }
);

and

my $schema = MyApp::Schema->connect(
    dsn           => 'dbi:SQLite:db/myapp.db',
    on_connect_do => 'PRAGMA foreign_keys = ON',
);

should do the trick.

As noted in comments, you can also use

on_connect_call => 'use_foreign_keys',

instead of

on_connect_do => 'PRAGMA foreign_keys = ON',

Problem

I want to enable foreign keys support to SQLite3 using DBIx::Class, in order to use cascade on update and on delete. I found this http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Storage/DBI/SQLite.pm in the documentation, but is not very clear on how to use it. This is how I'm setting the Schema.pm and the connection string in my script. ``` # Schema.pm package MyApp::Schema; use base qw/DBIx::Class::Schema/; use strict; use warnings; our $VERSION = '0.00001'; __PACKAGE__->load_namespaces(); __PACKAGE__->load_components(qw/Schema::Versioned/); __PACKAGE__->upgrade_directory('sql/'); # connection string in script use MyApp::Schema; my $schema = MyApp::Schema->connect('dbi:SQLite:db/myapp.db'); ``` Thanks,

Original source