instance method add_check_constraint

Ruby on Rails 8.0.4

Since v6.1.7.10

Available in: v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

add_check_constraint(table_name, expression, if_not_exists: false, **options)

Adds a new check constraint to the table. expression is a String representation of verifiable boolean condition.

add_check_constraint :products, "price > 0", name: "price_check"

generates:

ALTER TABLE "products" ADD CONSTRAINT price_check CHECK (price > 0)

The options hash can include the following keys:

:name

The constraint name. Defaults to chk_rails_<identifier>.

:if_not_exists

Silently ignore if the constraint already exists, rather than raise an error.

:validate

(PostgreSQL only) Specify whether or not the constraint should be validated. Defaults to true.

Parameters

table_name req
expression req
if_not_exists key = false
options keyrest
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1296
      def add_check_constraint(table_name, expression, if_not_exists: false, **options)
        return unless supports_check_constraints?

        options = check_constraint_options(table_name, expression, options)
        return if if_not_exists && check_constraint_exists?(table_name, **options)

        at = create_alter_table(table_name)
        at.add_check_constraint(expression, options)

        execute schema_creation.accept(at)
      end

Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 1296 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::SchemaStatements

Type at least 2 characters to search.

↑↓ navigate · open · esc close