instance method add_check_constraint

Ruby on Rails 6.1.7.10

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, **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>.

:validate

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

Parameters

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

        options = check_constraint_options(table_name, expression, 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 1152 · 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