# How do you use rails with (triggers and Oracle)?

**URL:** <https://www.ruby-forum.com/t/how-do-you-use-rails-with-triggers-and-oracle/85537>\
**Category:** Rails\
**Created:** [February 14, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/how-do-you-use-rails-with-triggers-and-oracle/85537 "2007-02-14T12:13:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [February 14, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/how-do-you-use-rails-with-triggers-and-oracle/85537/1 "2007-02-14T12:13:20Z")

</div>

I have an existing database in Oracle. All the tables have triggers  
that insert the next value from the table sequence into the id column.

I have told rails about the sequence using set\_sequence\_name.

The problem occurs when rails trys to insert a new row. It grabs the  
nextval from the sequence and uses that in its INSERT sql, BUT then the  
oracle trigger trigger automatically takes over (as it should) and grabs  
the next value from the sequence and forces it into the inserted row.  
This results in rails having the wrong id for that record. The id that  
rails has is minus one from what it should be.

My current solution is to minus one from the model’s id when i need to  
use it subsequently. This is not an ideal solution and is a real ball  
ache.

Is there any way around this problem with ActiveRecord? or is there a  
very clever way round it?

Any advice would be useful.

Thanks

Chris R.

---

<div class="post-metadata">

**Author:** ![crenner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/278dde/32.png) [@crenner](https://www.ruby-forum.com/u/crenner)\
**Post date:** [May 3, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/how-do-you-use-rails-with-triggers-and-oracle/85537/2 "2010-05-03T15:50:26Z")

</div>

Chris R. wrote:

> I have an existing database in Oracle. All the tables have triggers  
> that insert the next value from the table sequence into the id column.
> 
> I have told rails about the sequence using set\_sequence\_name.
> 
> The problem occurs when rails trys to insert a new row. It grabs the  
> nextval from the sequence and uses that in its INSERT sql, BUT then the  
> oracle trigger trigger automatically takes over (as it should) and grabs  
> the next value from the sequence and forces it into the inserted row.  
> This results in rails having the wrong id for that record. The id that  
> rails has is minus one from what it should be.
> 
> My current solution is to minus one from the model’s id when i need to  
> use it subsequently. This is not an ideal solution and is a real ball  
> ache.
> 
> Is there any way around this problem with ActiveRecord? or is there a  
> very clever way round it?
> 
> Any advice would be useful.
> 
> Thanks
> 
> Chris R.

I have the same problem. I’m using the oracle enhanced adapter v 1.2.4.  
The adapter does look for a present trigger with the default trigger  
name (i.e. \<table\_name\>\_PKT), but I don’t use default names so he  
doesn’t find it.

My current solution is to simply override the method  
has\_primary\_key\_trigger? that looks for the trigger:

```auto
module ActiveRecord
  module ConnectionAdapters
    class OracleEnhancedAdapter < AbstractAdapter
      def has_primary_key_trigger?(table_name, owner = nil, 
desc_table_name = nil, db_link = nil)
        true
      end
    end
  end
end
```

As I always use triggers, this works for me, but I know it isn’t the  
best solution.

Regards,

Denzel
