如何使用add_trigger方法在“DBIx :: Class”的帮助下在DB中创建触发器?

时间:2017-01-12 17:06:41

标签: perl dbix-class

我想在我的数据库中添加触发器。我使用DBIx::Class并按照以下示例操作:12 我的代码是:

package App::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_namespaces();

sub sqlt_deploy_hook {
    my ($self, $schema) = @_;
    $schema->add_trigger( name => 'foo' );
}

1;

但是我收到了这个错误:

Failed to translate to YAML: translate: Error with producer 'SQL::Translator::Producer::YAML': Can't call method "name" on an undefined value at /home/kes/work/projects/x/app/local/lib/perl5/SQL/Translator/Schema/Trigger.pm line 198

运行dbic-migration所需的所有环境变量的命令时:

dbic-migration --force --schema_class App::Schema --database PostgreSQL -Ilib prepare

我指向SQL::Translator::Schema::Trigger

我错过了什么?如何解决此错误?

UPD

即使我添加了所有参数,我也会收到错误:

Failed to translate to YAML: translate: Error with parser 'SQL::Translator::Parser::DBIx::Class': Table named users doesn't exist at /home/kes/work/projects/x/app/local/lib/perl5/SQL/Translator/Schema/Trigger.pm line 54

Here目标行:

my $table = $args->{schema}->get_table($arg)
   or die "Table named $arg doesn't exist";    

修改后的代码:

sub sqlt_deploy_hook {
    my ($self, $schema) = @_;

    warn "TABLES: " ,$schema->get_tables ,"\n";
    $schema->add_trigger(()
        ,name =>  'foo'
        ,perform_action_when => 'after'
        ,database_events     => 'insert'
        ,on_table => 'users'
        ,action => 'text'
        ,scope => 'row'
    );
}

此代码会产生下一个警告:

TABLES: users
TABLES: dbix_class_deploymenthandler_versions

但是DB目前只有一张桌子。我希望它至少应该产生:

TABLES: users dbix_class_deploymenthandler_versions

如何在DB创建触发器?

1 个答案:

答案 0 :(得分:0)

DBIx::Class::ResultSource::default_sqlt_deploy_hook可能存在问题:

  

最初设计用于期望Result类名称和正在部署的表的$ sqlt_table实例

随着工作在add_trigger之前添加下一行代码:

return   unless grep $_ eq 'users',  $schema->get_tables;

但推荐的方法是手动创建部署/升级/降级.sql文件