Skip to content

Instantly share code, notes, and snippets.

@jukkatupamaki
Last active January 18, 2022 21:43
Show Gist options
  • Select an option

  • Save jukkatupamaki/45317518687d9fb20b86ccfe6a170614 to your computer and use it in GitHub Desktop.

Select an option

Save jukkatupamaki/45317518687d9fb20b86ccfe6a170614 to your computer and use it in GitHub Desktop.

Revisions

  1. jukkatupamaki revised this gist Apr 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion knex-pg-check.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ knex.schema.raw(`
    `)
    ```

    Drop the above constraint (e.g. in "down" migration):
    Drop the above constraint (e.g. in a "down" migration):

    ```javascript
    knex.schema.raw(`
  2. jukkatupamaki created this gist Apr 2, 2020.
    24 changes: 24 additions & 0 deletions knex-pg-check.md
    Original 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
    `)
    ```