簡體   English   中英

如何使用add_trigger方法在`DBIx :: Class`的幫助下在數據庫中創建觸發器?

[英]How to create trigger in DB with help of `DBIx::Class` using add_trigger method?

我想將觸發器添加到數據庫中。 我用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

這里的目標線:

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創建觸發器?

DBIx :: Class :: ResultSource :: default_sqlt_deploy_hook可能存在問題:

最初旨在預期結果類名稱和要部署的表的$ sqlt_table實例

解決方法是在add_trigger之前添加下一行代碼:

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

但是推薦的方法是手動創建deploy / upgrade / downgrade .sql文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM