2017-01-12 22 views
1

我想添加触发器到我的数据库。我使用DBIx::Class并遵循以下示例:1,2
我的代码是:如何使用add_trigger方法在`DBIx :: Class`的帮助下在DB中创建触发器?

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中创建触发器?

回答

0

有可能与DBIx::Class::ResultSource::default_sqlt_deploy_hook问题:

which was originally designed to expect the Result class name and the $sqlt_table instance of the table being deployed

随着工作周围添加下一行代码add_trigger前:

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

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

相关问题