Last active
January 18, 2022 21:43
-
-
Save jukkatupamaki/45317518687d9fb20b86ccfe6a170614 to your computer and use it in GitHub Desktop.
Revisions
-
jukkatupamaki revised this gist
Apr 2, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ knex.schema.raw(` `) ``` Drop the above constraint (e.g. in a "down" migration): ```javascript knex.schema.raw(` -
jukkatupamaki created this gist
Apr 2, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ # How to use check constraints with Knex.js and Postgres Check constraints are useful for validating INSERT and UPDATE queries. With Knex.js, you have to use `raw` calls to add or drop constraints. Add a constraint to check that `my_column` in `my_table` is at least 0: ```javascript knex.schema.raw(` ALTER TABLE my_table ADD CONSTRAINT my_column_is_at_least_0 CHECK (my_column >= 0) `) ``` Drop the above constraint (e.g. in "down" migration): ```javascript knex.schema.raw(` ALTER TABLE my_table DROP CONSTRAINT my_column_is_at_least_0 `) ```