![](/img/trans.png)
[英]How to avoid race conditions when using the find_or_create method of DBIx::Class::ResultSet?
[英]How to create trigger in DB with help of `DBIx::Class` using add_trigger method?
我想將觸發器添加到數據庫中。 我用DBIx::Class
,並按照這些例子: 1 , 2 。
我的代碼是:
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.