# Unique index question

**URL:** <https://www.ruby-forum.com/t/unique-index-question/185100>\
**Category:** Rails\
**Created:** [March 13, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/unique-index-question/185100 "2010-03-13T18:10:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 13, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/unique-index-question/185100/1 "2010-03-13T18:10:08Z")

</div>

Hi in my development build using sqlite3, I specified email in my  
Account model to be a unique index as shown below, so why it the db  
happy adding the same emails? this type of insert should fail!

class CreateAccounts \< ActiveRecord::Migration  
def self.up  
create\_table :accounts do |t|  
t.string :email, :null=\>false, :limit=\>60  
t.index :email, :unique=\>true

I know about ‘validates\_uniqueness\_of :email’, but this check happen at  
the rails level, I wanted also something at the db level. I know sqlite3  
supports unique indexing, so what’s wrong?

–  
Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 13, 2010, 9:50pm UTC](https://www.ruby-forum.com/t/unique-index-question/185100/2 "2010-03-13T21:50:46Z")

</div>

On Mar 13, 2010, at 9:08 AM, Rajinder Y. wrote:

> Hi in my development build using sqlite3, I specified email in my  
> Account model to be a unique index as shown below, so why it the db  
> happy adding the same emails? this type of insert should fail!
> 
> class CreateAccounts \< ActiveRecord::Migration  
> def self.up  
> create\_table :accounts do |t|  
> t.string :email, :null=\>false, :limit=\>60  
> t.index :email, :unique=\>true

create\_table…  
…  
end

add\_index :accounts, :email, :unique =\> true

The “t.index” only works for change\_table()…

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 14, 2010, 4:41am UTC](https://www.ruby-forum.com/t/unique-index-question/185100/3 "2010-03-14T04:41:01Z")

</div>

Philip H. wrote:

> > ```
> > t.string :email, :null=>false, :limit=>60
> > t.index :email, :unique=>true
> > 
> > ```
> 
> create\_table…  
> …  
> end
> 
> add\_index :accounts, :email, :unique =\> true
> 
> The “t.index” only works for change\_table()…

Thank Philip for the explanation, this works =)
