DBIx :: Class如何为SQLite启用PRAGMA foreign_keys?

时间:2013-02-22 00:09:44

标签: perl sqlite dbix-class

我想使用DBIx :: Class为SQLite3启用外键支持,以便在更新和删除时使用级联。我在文档中找到了这个http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Storage/DBI/SQLite.pm,但对如何使用它并不是很清楚。

这就是我在脚本中设置Schema.pm和连接字符串的方法。

# 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');

谢谢,

1 个答案:

答案 0 :(得分:5)

两个

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

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

应该这样做。


如评论中所述,您也可以使用

on_connect_call => 'use_foreign_keys',

而不是

on_connect_do => 'PRAGMA foreign_keys = ON',