Class: ActiveRecord::ConnectionAdapters::PostgreSQLCheckConstraint

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

Overview

Creates CHECK 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 create a CHECK constraint:

  • on a column definition

  • on a table definition

  • when altering a table

Column Definition

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

Example

create_table(:foo) do |t|
  t.integer :fancy_id, :check => "fancy_id != 10"
end

# Produces:
#
# CREATE TABLE "foo" (
#   "id" serial primary key,
#   "fancy_id" integer DEFAULT NULL NULL,
#   CHECK (fancy_id != 10)
# );

You can also provide an Array to :check with multiple CHECK constraints. Each CHECK constraint can be either a String containing the CHECK expression or a Hash containing :name and :expression values if you want to provide a specific name for the constraint. Otherwise, PostgreSQL will provide a name automatically. Thus, the following is equivalent to the example above:

create_table(:foo) do |t|
  t.integer :fancy_id, :check => [ { :expression => "fancy_id != 10" } ]
end

See below for additional options.

Table Definition

CHECK constraints can also be applied to the table directly rather than on a column definition.

Examples

create_table(:foo) do |t|
  t.integer :fancy_id
  t.integer :another_fancy_id
  t.check_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,
#   CHECK (fancy_id != another_fancy_id)
# );

create_table(:foo) do |t|
  t.integer :fancy_id
  t.integer :another_fancy_id
  t.check_constraint 'fancy_id != another_fancy_id', :name => 'my_constraint'
end

# Produces:
#
# CREATE TABLE "foo" (
#   "id" serial primary key,
#   "fancy_id" integer DEFAULT NULL NULL,
#   "another_fancy_id" integer DEFAULT NULL NULL,
#   CONSTRAINT "my_constraint" CHECK (fancy_id != another_fancy_id)
# );

See below for additional options.

Table Manipulation

You can also create new CHECK constraints outside of a table definition using PostgreSQLAdapter#add_check_constraint.

Example

add_check_constraint(:foo, 'fancy_id != 10')

# Produces:
#
# ALTER TABLE "foo" ADD CHECK (fancy_id != 10);

See below for additional options.

CHECK Constraint Options

  • :name - specifies a name for the constraint.

  • :expression - when creating a column definition, you can supply either a String containing the expression or a Hash to supply both :name and :expression values.

  • :not_valid - adds the NOT VALID clause. Only useful when altering an existing table.

  • :no_inherit - adds the NO INHERIT clause.

Dropping CHECK Constraints

Like all PostgreSQL constraints, you can use PostgreSQLAdapter#drop_constraint to remove a constraint from a table.

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, expression, options = {}) ⇒ PostgreSQLCheckConstraint

:nodoc:



271
272
273
274
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 271

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

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



269
270
271
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 269

def expression
  @expression
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



276
277
278
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 276

def to_sql #:nodoc:
  "#{constraint_name}CHECK (#{expression})#{not_valid}#{no_inherit}"
end