# Composite Primary Keys and validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/composite-primary-keys-and-validates-uniqueness-of/171527>\
**Category:** Rails\
**Created:** [July 4, 2009, 3:50am UTC](https://www.ruby-forum.com/t/composite-primary-keys-and-validates-uniqueness-of/171527 "2009-07-04T03:50:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [July 4, 2009, 3:50am UTC](https://www.ruby-forum.com/t/composite-primary-keys-and-validates-uniqueness-of/171527/1 "2009-07-04T03:50:03Z")

</div>

This question regards the Composite Primary Keys gem  
([http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)) :

Once I use set\_primary\_keys, should I also use

validates\_uniqueness\_of to ensure that the composite primary key is  
unique and

attr\_readonly to ensure that the attributes in the primary key would  
not be overwritten?

Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [July 5, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/composite-primary-keys-and-validates-uniqueness-of/171527/2 "2009-07-05T20:20:15Z")

</div>

Learn by Doing wrote:

> This question regards the Composite Primary Keys gem  
> ([http://compositekeys.rubyforge.org/](http://compositekeys.rubyforge.org/)) :
> 
> Once I use set\_primary\_keys, should I also use
> 
> validates\_uniqueness\_of to ensure that the composite primary key is  
> unique

You could, but remember that the DB will catch that in any case.

> and
> 
> attr\_readonly to ensure that the attributes in the primary key would  
> not be overwritten?

Depends. If you’re using a composite key, presumably it’s built up from  
domain data, so in many cases, you wouldn’t want to make it read-only.

What is your data like that you think you need this?

> Thanks.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
