Class: ActiveRecord::ConnectionAdapters::PostgreSQLUniqueConstraint

Inherits:
PostgreSQLConstraint show all
Defined in:
lib/active_record/postgresql_extensions/constraints.rb

Overview

Creates UNIQUE constraints for PostgreSQL tables.

This class is meant to be used by PostgreSQL column and table definition and manipulation methods. There are several ways to use this class:

  • on a column definition

  • on a table definition

  • when altering a table

In PostgreSQL, a UNIQUE constraint is really just a unique index, so you can alternatively add a UNIQUE constraint using the standard ActiveRecord add_index method with the :unique option. You can also use our expanded PostgreSQLAdapter#create_index method, which adds additional PostgreSQL-specific options. See the PostgreSQLIndexDefinition class for details on these extra options.

Column Definition

When creating a new table via PostgreSQLAdapter#create_table, you can specify UNIQUE constraints on individual columns during definition.

Example

create_table(:foo) do |t|
  t.integer :fancy_id, :unique => true
end

# Produces:
#
# CREATE TABLE "foo" (
#   "id" serial primary key,
#   "fancy_id" integer DEFAULT NULL NULL,
#   UNIQUE ("fancy_id")
# );

You can provide additional options to the UNIQUE constraint by passing a Hash instead of true. See below for details on these additional options.

Table Definition

UNIQUE constraints can also be applied to the table directly rather than on a column definition. This is useful when you want to add multiple columns to the constraint.

Example

create_table(:foo) do |t|
  t.integer :fancy_id
  t.integer :another_fancy_id
  t.unique_constraint [ :fancy_id, :another_fancy_id ]
end

# Produces:
#
# CREATE TABLE "foo" (
#   "id" serial primary key,
#   "fancy_id" integer DEFAULT NULL NULL,
#   "another_fancy_id" integer DEFAULT NULL NULL,
#   UNIQUE ("fancy_id", "another_fancy_id")
# );

See below for additional options.

Table Manipulation

You can also create new UNIQUE constraints outside of a table definition using the standard ActiveRecord add_index method. You can also use our custom add_unique_constraint which adds a couple of PostgreSQL-specific options.

Additionally, since UNIQUE constraints in PostgreSQL are really just unique indexes, you can also use the the standard ActiveRecord add_index method with the :unique option or our custom PostgreSQLAdapter#create_index method similarly. The create_index method adds a couple of PostgreSQL-specific options if you need them.

Examples

# using the constraint method:
add_unique_constraint(:foo, [ :fancy_id, :another_fancy_id ])
# => ALTER TABLE "foo" ADD UNIQUE ("fancy_id", "another_fancy_id");

# using create_index:
create_index('my_index_name', :foo, [ :fancy_id, :another_fancy_id ], :unique => true)
# => CREATE UNIQUE INDEX "my_index_name" ON "foo"("fancy_id", "another_fancy_id");

# using the standard ActiveRecord add_index:
add_index(:foo, [ :fancy_id, :another_fancy_id ], :unique => true)
# => CREATE UNIQUE INDEX "index_foo_on_fancy_id_and_another_fancy_id" ON "foo" ("fancy_id", "another_fancy_id");

You’ll notice that in create_index we must manually supply a name while add_index can generate one for us automatically. See the create_index documentation for details as to why this mysterious departure from the standard ActiveRecord method is necessary.

Options for UNIQUE Constraints

When creating UNIQUE constraints using a column or table definition or when using add_unique_constraint, there are a hanful of PostgreSQL-specific options that you may find useful.

  • :name - specifies a name for the constraint.

  • :storage_parameters - PostgreSQL allows you to add a couple of additional parameters to indexes to govern disk usage and such. This option is a simple String or a Hash that lets you insert these options as necessary. See the PostgreSQL documentation on index storage parameters for details. :index_parameters can also be used.

  • :tablespace - allows you to specify a tablespace for the unique index being created. See the PostgreSQL documentation on tablespaces for details.

Dropping UNIQUE Constraints

Like all PostgreSQL constraints, you can use drop_constraint to remove a constraint from a table. Since a UNIQUE constraint is really just a unique index in PostgreSQL, you can also use the standard ActiveRecord remove_index method or our custom PostgreSQLAdapter#drop_index method.

With drop_index, you can provide a couple of PostgreSQL-specific options, which may be useful in some situations. See the documentation for PostgreSQLAdapter#drop_index for details.

Instance Attribute Summary collapse

Attributes inherited from PostgreSQLConstraint

#base, #options

Instance Method Summary collapse

Methods included from PostgreSQLExtensions::Utils

#hash_or_array_of_hashes, #options_from_hash_or_string, #strip_heredoc

Constructor Details

#initialize(base, columns, options = {}) ⇒ PostgreSQLUniqueConstraint

:nodoc:



411
412
413
414
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 411

def initialize(base, columns, options = {}) #:nodoc:
  @columns = columns
  super(base, options)
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



409
410
411
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 409

def columns
  @columns
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



416
417
418
419
420
421
422
423
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 416

def to_sql #:nodoc:
  sql = "#{constraint_name}UNIQUE ("
  sql << Array.wrap(columns).collect { |c| base.quote_column_name(c) }.join(', ')
  sql << ")"
  sql << storage_parameters
  sql << using_tablespace
  sql
end