OCI8 + Activerecord,如何创建触发器
OCI8 + Activerecord, How to create trigger
我一直在处理这个问题。
我正在使用 active record + OCI8 + Sinatra,我无法 运行 迁移(因为我无权访问数据库上的管理员用户,但我可以向我的 oracle 管理员发送查询)。
这是我的模型
class Modify < ActiveRecord::Base
self.sequence_name = "PRM_W01.seq_modifies"
belongs_to :parameter
end
但是当我尝试创建一个新用户时,我得到了这个输出
D, [2015-08-31T16:50:34.636767 #4696] DEBUG -- : Primary Key (7.5ms) SELECT cc.column_name FROM all_constraints c, all_cons_columns cc WHERE c.owner = 'PRM_W01' AND c.table_name = 'MODIFIES' AND c.constraint_type = 'P' AND cc.owner = c.owner AND cc.constraint_name = c.constraint_name
D, [2015-08-31T16:50:34.639891 #4696] DEBUG -- : Primary Key Trigger (2.5ms) SELECT trigger_name
FROM all_triggers
WHERE owner = 'PRM_W01'
AND trigger_name = 'MODIFIES_PKT'
AND table_owner = 'PRM_W01'
AND table_name = 'MODIFIES'
AND status = 'ENABLED'
OCIError: ORA-00942: table or view does not exist
我正在阅读this
Create primary key trigger (so that you can skip primary key value in
INSERT statement). By default trigger name will be “table_name_pkt“,
you can override the name with :trigger_name option (but it is not
recommended to override it as then this trigger will not be detected
by ActiveRecord model and it will still do prefetching of sequence
value).
所以我认为我的问题是我没有创建触发器,但我不知道如何迁移,有人可以帮助我获取该代码吗?
我已经解决了在 db 上创建触发器的问题。
CREATE OR REPLACE TRIGGER PRM_W01.MODIFIES_PKT
BEFORE INSERT ON PRM_W01.MODIFIES FOR EACH ROW
BEGIN
IF inserting THEN
IF :new.id IS NULL THEN
SELECT PRM_W01.SEQ_MODIFIES.NEXTVAL INTO :new.id FROM dual;
END IF;
END IF;
END;
我阅读了 oracle-enhanced 的代码来创建它。
为了在我自己的开发环境中获得 oracle 运行,我发现的另一个很酷的事情是 this docker image
我一直在处理这个问题。 我正在使用 active record + OCI8 + Sinatra,我无法 运行 迁移(因为我无权访问数据库上的管理员用户,但我可以向我的 oracle 管理员发送查询)。
这是我的模型
class Modify < ActiveRecord::Base
self.sequence_name = "PRM_W01.seq_modifies"
belongs_to :parameter
end
但是当我尝试创建一个新用户时,我得到了这个输出
D, [2015-08-31T16:50:34.636767 #4696] DEBUG -- : Primary Key (7.5ms) SELECT cc.column_name FROM all_constraints c, all_cons_columns cc WHERE c.owner = 'PRM_W01' AND c.table_name = 'MODIFIES' AND c.constraint_type = 'P' AND cc.owner = c.owner AND cc.constraint_name = c.constraint_name
D, [2015-08-31T16:50:34.639891 #4696] DEBUG -- : Primary Key Trigger (2.5ms) SELECT trigger_name
FROM all_triggers
WHERE owner = 'PRM_W01'
AND trigger_name = 'MODIFIES_PKT'
AND table_owner = 'PRM_W01'
AND table_name = 'MODIFIES'
AND status = 'ENABLED'
OCIError: ORA-00942: table or view does not exist
我正在阅读this
Create primary key trigger (so that you can skip primary key value in INSERT statement). By default trigger name will be “table_name_pkt“, you can override the name with :trigger_name option (but it is not recommended to override it as then this trigger will not be detected by ActiveRecord model and it will still do prefetching of sequence value).
所以我认为我的问题是我没有创建触发器,但我不知道如何迁移,有人可以帮助我获取该代码吗?
我已经解决了在 db 上创建触发器的问题。
CREATE OR REPLACE TRIGGER PRM_W01.MODIFIES_PKT
BEFORE INSERT ON PRM_W01.MODIFIES FOR EACH ROW
BEGIN
IF inserting THEN
IF :new.id IS NULL THEN
SELECT PRM_W01.SEQ_MODIFIES.NEXTVAL INTO :new.id FROM dual;
END IF;
END IF;
END;
我阅读了 oracle-enhanced 的代码来创建它。
为了在我自己的开发环境中获得 oracle 运行,我发现的另一个很酷的事情是 this docker image