# Legacy DB table with double primary key

**URL:** <https://www.ruby-forum.com/t/legacy-db-table-with-double-primary-key/134912>\
**Category:** Rails\
**Created:** [April 16, 2008, 4:22am UTC](https://www.ruby-forum.com/t/legacy-db-table-with-double-primary-key/134912 "2008-04-16T04:22:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 16, 2008, 4:22am UTC](https://www.ruby-forum.com/t/legacy-db-table-with-double-primary-key/134912/1 "2008-04-16T04:22:36Z")

</div>

I have a legacy MySQL table with a double key:

describe amethyst\_development.dspam\_token\_data;  
±--------------±---------------------±-----±----±-----------±------+  
| Field | Type | Null | Key | Default | Extra |  
±--------------±---------------------±-----±----±-----------±------+  
| uid | smallint(5) unsigned | NO | PRI | 0 | |  
| token | bigint(20) unsigned | NO | PRI | | |  
| spam\_hits | int(11) | NO | | 0 | |  
| innocent\_hits | int(11) | NO | | 0 | |  
| last\_hit | date | NO | | 0000-00-00 | |  
±--------------±---------------------±-----±----±-----------±------+

How do I define the primary key in the model class?

class Token \< ActiveRecord::Base  
set\_table\_name ‘dspam\_token\_data’  
set\_primary\_key [‘uid’, ‘token’]  
end

Gives the error:

> > t.save  
> > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘uidtoken’  
> > in ‘where clause’: UPDATE `dspam_token_data` SET `spam_hits` = 10,  
> > `last_hit` = ‘2008-03-19’, `innocent_hits` = 64, `token` =  
> > 3426742412487913678, `uid` = 1 WHERE `uidtoken` = NULL

TIA,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 16, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/legacy-db-table-with-double-primary-key/134912/2 "2008-04-16T18:06:34Z")

</div>

maybe google ‘ruby composite keys’?

> How do I define the primary key in the model class?
> 
> class Token \< ActiveRecord::Base  
> set\_table\_name ‘dspam\_token\_data’  
> set\_primary\_key [‘uid’, ‘token’]  
> end

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 16, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/legacy-db-table-with-double-primary-key/134912/3 "2008-04-16T21:07:04Z")

</div>

So Rails doesn’t but some one has written a Gem to add it.

Thank you,  
Jeffrey

Quoting Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
