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:
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 a "down" migration):
knex.schema.raw(`
ALTER TABLE my_table
DROP CONSTRAINT my_column_is_at_least_0
`)
I couldn't find anything on check constraints in the Knex.js docs. This helped fill in the cracks of my understanding. Thank you.