# Migration: 'unique' column constraint

**URL:** <https://www.ruby-forum.com/t/migration-unique-column-constraint/108450>\
**Category:** Rails\
**Created:** [July 3, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/migration-unique-column-constraint/108450 "2007-07-03T17:01:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [July 3, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/migration-unique-column-constraint/108450/1 "2007-07-03T17:01:03Z")

</div>

I’m using a legacy sqlite3 database for an app I’m developing. I’m  
trying to create a migration that creates a table containing a column  
with a ‘unique’ constraint. Here’s my migration:

class CreateEvents \< ActiveRecord::Migration  
def self.up  
create\_table :events do |t|  
#execute “name text NOT NULL UNIQUE”  
t.column :name, :text, :null =\> false, :unique =\> true  
t.column :cmd, :text, :null =\> false  
t.column :valid, :integer, :null =\> false  
t.column :arg1, :text  
t.column :arg2, :text  
t.column :arg3, :text  
t.column :arg4, :text  
t.column :arg5, :text  
end  
end

def self.down  
drop\_table :events  
end  
end

The :unique constraint was quietly ignored by the migration, so the name  
column ended up looking like this: “name text NOT NULL”. I also tried  
executing the equivalent sql in place of the “t.column :name” statement,  
but received an invalid sql error: “SQLite3::SQLException: near “name”:  
syntax error: name text NOT NULL UNIQUE”.

I can’t create the column after the table creation becaues sqlite3  
doesn’t support the ‘unique’ constraint in an “ALTER TABLE” statement.

Any ideas on how I can make the ‘name’ column unique? This must be done  
in the schema, not in the model, because other apps already use the  
current schema.

Thanks,  
Earle
