Merge pull request #48019 from agrobbin/postgresql-exclusion-constraints-guides

Add a section on exclusion constraints to the AR PostgreSQL guide
This commit is contained in:
Yasuo Honda 2023-04-28 08:48:09 +09:00 committed by GitHub
commit cc359c077f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,7 @@ After reading this guide, you will know:
* How to include non-key columns in indexes.
* How to use deferrable foreign keys.
* How to use unique constraints.
* How to implement exclusion constraints.
* How to implement full text search with PostgreSQL.
* How to back your Active Record models with database views.
@ -651,6 +652,23 @@ add_unique_key :items, deferrable: :deferred, using_index: "index_items_on_posit
Like foreign keys, unique constraints can be deferred by setting `:deferrable` to either `:immediate` or `:deferred`. By default, `:deferrable` is `false` and the constraint is always checked immediately.
Exclusion Constraints
---------------------
* [exclusion constraints](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION)
```ruby
# db/migrate/20131220144913_create_products.rb
create_table :products do |t|
t.integer :price, null: false
t.daterange :availability_range, null: false
t.exclusion_constraint "price WITH =, availability_range WITH &&", using: :gist, name: "price_check"
end
```
Like foreign keys, exclusion constraints can be deferred by setting `:deferrable` to either `:immediate` or `:deferred`. By default, `:deferrable` is `false` and the constraint is always checked immediately.
Full Text Search
----------------