如何使用PostgreSQL触发器?
我正在尝试在我的 Rails 应用程序中使用 PostgreSQL 触发器。因此,我尝试使用此迁移,其中触发器的执行应该很容易:
-- class AddTriggersToProducts < ActiveRecord::Migration
def self.up
table :products
execute %q{
create trigger trig1 before insert on products for each row
begin
price = price + 5
end;
}
end
def self.down
execute 'DROP TRIGGER trig1'
end
end
但这并没有改变任何内容。如果我要在这里使用过程或函数,我不知道在哪里编写过程或函数......
I am trying to use PostgreSQL triggers in my rails app. So I tried using this migration where execution of triggers is supposedly easy:
-- class AddTriggersToProducts < ActiveRecord::Migration
def self.up
table :products
execute %q{
create trigger trig1 before insert on products for each row
begin
price = price + 5
end;
}
end
def self.down
execute 'DROP TRIGGER trig1'
end
end
But this didn't change anything. I don't know where to write the procedure or function if I am going to use one here ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“创建触发器”由两个步骤组成在 PostgreSQL 中:
1.) 创建一个触发函数< /a> - 具有特殊返回值
trigger
:多个触发器可以使用相同的触发函数。
2.) 创建触发器调用现有的触发器函数:
要“删除触发器”(意味着触发函数),您必须首先删除引用它的所有触发器,然后删除触发器函数本身。
如果删除一个表,所有附加的触发器也会随之删除。无需单独删除它们。
"Creating a trigger" consists of two steps in PostgreSQL:
1.) Create a trigger function - with special return value
trigger
:Multiple triggers can use the same trigger function.
2.) Create a trigger calling an existing trigger function:
To "drop the trigger" (meaning the trigger function), you have to first drop all triggers referencing it and then drop the trigger function itself.
If you drop a table, all attached triggers are dropped with it. No need to drop those separately.
这样的东西有用吗?创建一个函数,然后为触发器执行该函数:
Does something like this work? Creating a function and then executing the function for the trigger:
hair_trigger gem 是管理触发器创建的好方法。
这是 Hair_trigger 文档中的示例:
The hair_trigger gem is a nice way to manage the creation of triggers.
Here is an example from hair_trigger's docs: