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
`)
NOT ALTER, but anyway. Tricky way while create
t.specificType('hash', 'varchar(128) check(length(hash) = 128)')