mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Show Reconstructed Constraints For A Table as a Postgres TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1625 TILs and counting..._
|
_1626 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
||||||
@@ -912,6 +912,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
|
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
|
||||||
- [Shorthand Absolute Value Operator](postgres/shorthand-absolute-value-operator.md)
|
- [Shorthand Absolute Value Operator](postgres/shorthand-absolute-value-operator.md)
|
||||||
- [Show All Versions Of An Operator](postgres/show-all-versions-of-an-operator.md)
|
- [Show All Versions Of An Operator](postgres/show-all-versions-of-an-operator.md)
|
||||||
|
- [Show Reconstructed Constraints For A Table](postgres/show-reconstructed-constraints-for-a-table.md)
|
||||||
- [Show The Hidden Queries Behind Backslash Commands](postgres/show-the-hidden-queries-behind-backslash-commands.md)
|
- [Show The Hidden Queries Behind Backslash Commands](postgres/show-the-hidden-queries-behind-backslash-commands.md)
|
||||||
- [Sleeping](postgres/sleeping.md)
|
- [Sleeping](postgres/sleeping.md)
|
||||||
- [Special Math Operators](postgres/special-math-operators.md)
|
- [Special Math Operators](postgres/special-math-operators.md)
|
||||||
|
|||||||
35
postgres/show-reconstructed-constraints-for-a-table.md
Normal file
35
postgres/show-reconstructed-constraints-for-a-table.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Show Reconstructed Constraints For A Table
|
||||||
|
|
||||||
|
The [`pg_get_constraintdef`
|
||||||
|
function](https://pgpedia.info/p/pg_get_constraintdef.html) can be used to
|
||||||
|
reconstruct the command for creating a given constraint. This isn't necessarily
|
||||||
|
the command (or commands) that originally created the constraint, but rather a
|
||||||
|
reconstruction.
|
||||||
|
|
||||||
|
We have to pass it an `oid` that corresponds to the constraint which we can get
|
||||||
|
from the `pg_constraint` table. These results can be further narrowed down by
|
||||||
|
the `conname` (constraint name) and `conrelid` (table name).
|
||||||
|
|
||||||
|
Here is an example of listing the constraints on a `reading_statuses` table.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select
|
||||||
|
conname,
|
||||||
|
pg_get_constraintdef(oid)
|
||||||
|
from pg_constraint
|
||||||
|
where conrelid = 'reading_statuses'::regclass;
|
||||||
|
|
||||||
|
conname | pg_get_constraintdef
|
||||||
|
-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
reading_statuses_pkey | PRIMARY KEY (id)
|
||||||
|
fk_rails_17ee7cb2c4 | FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
fk_rails_0d3729339f | FOREIGN KEY (book_id) REFERENCES books(id)
|
||||||
|
reading_statuses_valid_status_check | CHECK (((status)::text = ANY ((ARRAY['started'::character varying, 'completed'::character varying, 'abandoned'::character varying, 'already_read'::character varying])::text[])))
|
||||||
|
(4 rows)
|
||||||
|
```
|
||||||
|
|
||||||
|
I came across this while experimenting with [an idea for a fail-fast Rails
|
||||||
|
initializer
|
||||||
|
check](https://gist.github.com/jbranchaud/12813a0558f9cd06bcc24b7d8706550c)
|
||||||
|
that verifies the values of the `reading_statuses_valid_status_check` stay in
|
||||||
|
sync with the Rails version of those values that live in a constant.
|
||||||
Reference in New Issue
Block a user