# Existing Oracle schema with its own sequence

**URL:** <https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930>\
**Category:** Rails\
**Created:** [August 13, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930 "2007-08-13T16:38:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [August 13, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/1 "2007-08-13T16:38:20Z")

</div>

I’m mapping a new RoR application onto an existing Oracle schema. The  
tables _usually_ have a primary key named _id_, but the sequences that  
assign new id values don’t have Rails-convention names. Is there a way  
to tell Rails that the sequence for table FOO is not FOO\_SEQ, but  
XYZ\_SEQ?

If that’s not possible, is there a way to create the equivalent of a  
table’s _view_, but for a sequence?

Looking forward to any suggestions. Thanks.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [August 13, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/2 "2007-08-13T17:07:25Z")

</div>

I got an answer from Railsfrance: set\_sequence\_name(value = nil,  
&block) can be used. I hadn’t been able to find the word “sequence” in  
the index of the AWDwR2 book…

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [August 13, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/3 "2007-08-13T19:09:39Z")

</div>

On Aug 13, 8:06 am, gers32 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I got an answer from Railsfrance: set\_sequence\_name(value = nil,  
> &block) can be used. I hadn’t been able to find the word “sequence” in  
> the index of the AWDwR2 book…

i think you want to Google for “legacy app” or “legacy schema”  
There’s quite a few blogs and threads here.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [September 11, 2007, 3:25am UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/4 "2007-09-11T03:25:10Z")

</div>

I have the same question (i.e., conflicts with oracle triggers and  
rails). Also, I am finding that I have the following problem using  
legacy sequences with non-rails names:

Even if I do set\_sequence\_name in the model, and even if I create the  
proper sequences in my migrations, when I run rake test, rails wipes  
out my test database and recreates it without the custom sequences  
(though they exist in the development db), so I always get a “sequence  
does not exist” in my create tests.

Is there some way to use the test\_helper to hook into the test case  
scenarios and fix this before the tests run?

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [September 11, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/5 "2007-09-11T19:00:20Z")

</div>

Here’s my solution: [weeblog: Non-Standard  
Sequences and Rails Tests](http://weenet.blogspot.com/2007/09/non-%0Astandard-sequences-and-rails-tests.html#links)

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [August 14, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/6 "2007-08-14T15:28:09Z")

</div>

Yes, I did search the site for these keywords and other combinations.  
I couldn’t find an answer to my follow-up question:

The legacy Oracle schema uses triggers to get the sequences’ next  
values. Isn’t there a potential conflict with Rails’ mechanism during  
record creation?

If so, should I tell Rails to use the existing trigger mechanism (and  
how), or should I deactivate the triggers exclusively for my  
application - the tables are being accessed by other legacy apps -  
(and how)?

Last thought : I’m going to create views, so that I can use the Rails  
naming conventions. Maybe that solves my problem, assuming I can link  
the sequence to the view?

You can tell I’m new both to RoR and Oracle, can’t you…

Thanks, Chris.

Le mécanisme d’affectation des clés primaires n’est pas clair, dans le  
contexte de ma base existante : celle-ci utilise des “triggers” pour  
récupérer la nouvelle valeur dans la séquence. Comment éviter un  
conflit entre ceux-ci et Rails, lors de la création d’un nouvel  
enregistrement ? Dois-je dire à Rails d’utiliser le mécanisme existant  
(via “triggers”) et comment ? Ou alors, dois-je inactiver les  
“triggers” pour mon application (sachant que d’autres applis ont  
accèsà la même base) et comment ?

RoR et Oracle sont nouveaux pour moi ; ça se voit, non ?

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [September 11, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/existing-oracle-schema-with-its-own-sequence/111930/7 "2007-09-11T19:02:20Z")

</div>

Here’s the solution I came up with (for the testing issue):

[http://weenet.blogspot.com/2007/09/non-standard-sequences-and-rails-tests.html#links](http://weenet.blogspot.com/2007/09/non-standard-sequences-and-rails-tests.html#links)
