# Legacy DB and id issue

**URL:** <https://www.ruby-forum.com/t/legacy-db-and-id-issue/168625>\
**Category:** Rails\
**Created:** [May 23, 2009, 3:13am UTC](https://www.ruby-forum.com/t/legacy-db-and-id-issue/168625 "2009-05-23T03:13:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 23, 2009, 3:13am UTC](https://www.ruby-forum.com/t/legacy-db-and-id-issue/168625/1 "2009-05-23T03:13:16Z")

</div>

ruby: 1.8.6  
rails: 2.3.2  
Database: Oracle 10g

Table with fields:  
ID varchar2(6)  
REC\_NO number(10,0)  
(No primary key declaration)

Model:  
class Myclass \< ActiveRecord::Base  
self.primary\_key = ‘rec\_no’  
self.sequence\_name = ‘s\_rn\_mytable’  
end

As you can see as described in the model there is a sequence in the DB  
that generates a unique number.

REC\_NO is being used as “primary key” and should be receiving the  
unique generated value.

ID is used to store values (not unique) from another table.

When a record is created the value of ID is always nil with the model  
above. If I change the primary key description in the model and  
specify the column ID (self.primary\_key = ‘id’) I can assign values to  
ID (although a varchar column the stored numbers are numeric so it  
lets me do it) but then REC\_NO gets the value nil.

Is there a way to make this work the ‘rails’ way? I’d rather not use  
SQL, which allows me to create the records the way I need.

Thanks a lot.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [May 23, 2009, 5:40am UTC](https://www.ruby-forum.com/t/legacy-db-and-id-issue/168625/2 "2009-05-23T05:40:36Z")

</div>

Corrections:

If I change the primary key description in the model and specify the  
column ID (self.primary\_key = ‘id’) I can assign values to ID, the  
model shows the values I entered but they are overridden anyway by the  
DB.

Pepe
