Jarl Friis wrote:
> Philip Hallstrom <[email protected]> writes:
> 
>>> CREATE TABLE properties (
>>
>> add_index :properties, [:namespace, :name], :unique => true
> 
> After trying this and opening my interactive SQL prompt (psql), I can
> see that this only creates an index on the table not a table
> constraint. I can still put duplicate rows in the table.

Excerpt from the PostgreSQL manual:
----------------------------------
PostgreSQL automatically creates a unique index when a unique constraint 
or a primary key is defined for a table. The index covers the columns 
that make up the primary key or unique columns (a multicolumn index, if 
appropriate), and is the mechanism that enforces the constraint.
----------------------------------

>From what I gather using "add_index :properties, [:namespace, :name], 
:unique => true" should do pretty much the same thing as adding a unique 
constraint, and do so in a database agnostic manner.

If you really want to use the constraint then simply execute the SQL 
yourself:

Example:

  CREATE TABLE properties (
    namespace CHAR(50),
    name      CHAR(50),
    value     VARCHAR(100),
  );
  execute <<-SQL
    ALTER TABLE products
    ADD CONSTRAINT my_constraint UNIQUE (namespace, name)
  SQL

Note: Don't forget to drop the constraint in your down method if 
necessary.
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to