Skip to content

Commit 2d8c349

Browse files
committed
add a section on exclusion constraints to the AR PostgreSQL guide
1 parent eac3208 commit 2d8c349

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

guides/source/active_record_postgresql.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ After reading this guide, you will know:
1212
* How to include non-key columns in indexes.
1313
* How to use deferrable foreign keys.
1414
* How to use unique constraints.
15+
* How to implement exclusion constraints.
1516
* How to implement full text search with PostgreSQL.
1617
* How to back your Active Record models with database views.
1718

@@ -651,6 +652,23 @@ add_unique_key :items, deferrable: :deferred, using_index: "index_items_on_posit
651652

652653
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.
653654

655+
Exclusion Constraints
656+
---------------------
657+
658+
* [exclusion constraints](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION)
659+
660+
```ruby
661+
# db/migrate/20131220144913_create_products.rb
662+
create_table :products do |t|
663+
t.integer :price, null: false
664+
t.daterange :availability_range, null: false
665+
666+
t.exclusion_constraint "price WITH =, availability_range WITH &&", using: :gist, name: "price_check"
667+
end
668+
```
669+
670+
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.
671+
654672
Full Text Search
655673
----------------
656674

0 commit comments

Comments
 (0)