class ActiveRecord::ConnectionAdapters::PostgreSQLExcludeConstraint
Creates EXCLUDE constraints for PostgreSQL tables and columns.
This class is meant to be used by PostgreSQL column and table definition and manipulation methods. There are two ways to create a EXCLUDE constraint:
-
on a table definition
-
when altering a table
In both cases, a Hash or an Array of Hashes should be used to set the EXCLUDE constraint checks. The Hash(es) should be in the format { :element => ..., :with => ... }
, where :element
is a column name or expression and :with
is the operator to compare against. The key :operator
is an alias for :where
.
Table Definition¶ ↑
EXCLUDE constraints can also be applied to the table directly rather than on a column definition.
Example¶ ↑
The following example produces the same result as above:
create_table('foo') do |t| t.integer :blort t.exclude({ :element => 'length(blort)', :with => '=' }, { :name => 'exclude_blort_length' }) end # Produces: # # CREATE TABLE "foo" ( # "id" serial primary key, # "blort" text, # CONSTRAINT "exclude_blort_length" EXCLUDE (length(blort) WITH =) # );
Table Manipulation¶ ↑
You can also create new EXCLUDE constraints outside of a table definition using PostgreSQLAdapter#add_exclude_constraint
.
Examples¶ ↑
add_exclude_constraint(:foo, { :element => :bar_id, :with => '=' }) # => ALTER TABLE "foo" ADD EXCLUDE ("bar_id" WITH =);
Options for EXCLUDE Constraints¶ ↑
-
:name
- specifies a name for the constraint. -
:using
- sets the index type to be used. Usually this will:gist
, but the default is left blank to allow for the PostgreSQL default which is:btree
. See the PostgreSQL docs for details. -
: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 index being created. See the PostgreSQL documentation on tablespaces for details. -
:conditions
- sets the WHERE conditions for the EXCLUDE constraint. You can also use the:where
option.
Dropping EXCLUDE Constraints¶ ↑
Like all PostgreSQL constraints, you can use PostgreSQLAdapter#drop_constraint
to remove a constraint from a table.