[英]How to create trigger in DB with help of `DBIx::Class` using add_trigger method?
I want to add trigger into my database. 我想将触发器添加到数据库中。 I use
DBIx::Class
and follow these examples: 1 , 2 . 我用
DBIx::Class
,并按照这些例子: 1 , 2 。
My code is: 我的代码是:
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; 1;
But I get this error: 但是我得到这个错误:
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
When run command with all environment variables as required by dbic-migration
: 使用
dbic-migration
要求的所有环境变量运行命令时:
dbic-migration --force --schema_class App::Schema --database PostgreSQL -Ilib prepare
Which point me somewhere into SQL::Translator::Schema::Trigger
这使我进入了
SQL::Translator::Schema::Trigger
What did I miss? 我错过了什么? How to fix this error?
如何解决这个错误?
UPD UPD
Even when I add all arguments I got error: 即使我添加了所有参数,我仍然出错:
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";
Modified code: 修改后的代码:
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'
);
}
This code produce next warnings: 此代码会产生下一条警告:
TABLES: users
TABLES: dbix_class_deploymenthandler_versions
But DB has only one table at the moment. 但是DB目前只有一张表。 I expect it at least should produce:
我希望至少应该产生:
TABLES: users dbix_class_deploymenthandler_versions
How to create trigger in DB
? 如何在
DB
创建触发器?
There maybe the problem with DBIx::Class::ResultSource::default_sqlt_deploy_hook : 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
最初旨在预期结果类名称和要部署的表的$ sqlt_table实例
As work around add next line of code before add_trigger
: 解决方法是在
add_trigger
之前添加下一行代码:
return unless grep $_ eq 'users', $schema->get_tables;
But the recommend way is to create deploy/upgrade/downgrade .sql
files manually 但是推荐的方法是手动创建deploy / upgrade / downgrade
.sql
文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.