Unique index question

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.

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()…

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 =)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs