# Primary\_key / legacy db

**URL:** <https://www.ruby-forum.com/t/primary-key-legacy-db/171332>\
**Category:** Rails\
**Created:** [June 30, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/primary-key-legacy-db/171332 "2009-06-30T19:39:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [June 30, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/primary-key-legacy-db/171332/1 "2009-06-30T19:39:52Z")

</div>

Hi,  
I am forced to use an old database with rails.

Relevant db structure;

mysql\> select id,permission,incid from acl group by id;  
±—±-----------±------+  
| id | permission | incid |  
±—±-----------±------+  
| 1 | 58 | 1 |  
| 2 | 57 | 25 |  
| 3 | 57 | 32 |  
±—±-----------±------+

Show create table acl:

CREATE TABLE `acl` (  
id`int(4) NOT NULL, .....`incid`int(11) NOT NULL auto_increment, PRIMARY KEY (`id`,`type`,`seq`,`incid`), UNIQUE KEY`incid`(`incid`)

So the primary key that I want to use in place of id is “incid”

“id” contains some acl ids and is not auto increment (has multiple  
rows returned per one `id`).

My model looks like this:

class Acl \< ActiveRecord::Base  
set\_table\_name :acl;  
set\_primary\_key :incid;  
def self.primary\_key() ‘incid’; end  
self.primary\_key = ‘incid’;  
set\_inheritance\_column :ruby\_id;  
end

script/console

> > r = Acl.find(2); \<-- that’s checking for `incid`  
> > ?\> r.id  
> > =\> 2
> 
> >

log:  
Acl Load (0.3ms) SELECT \* FROM `acl` WHERE (`acl`.`incid` = 2)

script/console

> > r = Acl.find(31);  
> > ?\> r.id  
> > =\> 31  
> > Acl Load (0.3ms) SELECT \* FROM `acl` WHERE (`acl`.`incid` = 31)

So, instead of r.id returning the value of the `id` field , it returns  
the value for the incid field.

For the second example it should have returned `2` instead of `31`  
which is actually the value of `incid`

mysql\> select id,permission,incid from acl where incid=‘31’;  
±—±-----------±------+  
| id | permission | incid |  
±—±-----------±------+  
| 2 | 57 | 31 |  
±—±-----------±------+

What should my model look like to be able to get the value of the `id`  
field?

Dan

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [June 30, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/primary-key-legacy-db/171332/2 "2009-06-30T21:38:11Z")

</div>

On Jun 30, 2009, at 10:29 AM, dan wrote:

> | 2 | 57 | 25 |  
> UNIQUE KEY `incid` (`incid`)  
> set\_primary\_key :incid;
> 
> > >
> 
> | 2 | 57 | 31 |  
> ±—±-----------±------+
> 
> What should my model look like to be able to get the value of the `id`  
> field?
> 
> Dan

See if you can get the field that you want with this syntax:

console\> r=Acl.find(31); r[:id]

r.id will always be the primary key (as you’ve discovered), but r[:id]  
(or r[‘id’] if you prefer) will give you the ‘id’ attribute. (I  
recently had to refer to a column ‘type’ this way because ruby wants  
r.type to be the same as r.class, and r[:type] always returns the  
column.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
