Skip to content

Constraint Operations

Operation: addConstraint

pgm.addConstraint( tablename, constraint_name, expression )

IMPORTANT

Add a named column constraint - postgres docs

Alias: createConstraint

Arguments

NameTypeDescription
tablenameNameName of the table to alter
constraint_namestringName for the constraint
expressionstring or objectConstraint expression (raw sql) or definition -- see constraint definition section

Constraint Definition

OptionTypeDescription
checkstring or arraySQL for a check constraint(s)
uniqueName or array of Names or array of arrays of NamesNames of unique columns
primaryKeyName or array of NamesNames of primary columns
excludestringSQL for an exclude constraint
deferrablebooleanFlag for deferrable table constraint
deferredbooleanFlag for initially deferred deferrable table constraint
commentstringComment on a singular, named constraint
foreignKeysobject or array of objectsForeign keys specification -- see foreign keys section

Foreign Keys

OptionTypeDescription
columnsName or array of NamesNames of columns
referencesNameNames of foreign table and column names
referencesConstraintNamestringName of the created constraint (only necessary when creating multiple constraints)
referencesConstraintCommentstringComment on the individual foreign key constraint
onDeletestringAction to perform on delete
onUpdatestringAction to perform on update
matchstringFULL or SIMPLE

Reverse Operation: dropConstraint

pgm.dropConstraint( tablename, constraint_name, options )

IMPORTANT

Drop a named column constraint - postgres docs

Arguments

NameTypeDescription
tablenameNameName of the table to alter
constraint_namestringName of the constraint
optionsobjectCheck below for available options

Options

OptionTypeDescription
ifExistsbooleanAdds IF EXISTS to DROP CONSTRAINT so a missing constraint does not error
ifTableExistsbooleanAdds IF EXISTS to ALTER TABLE so a missing table does not error
cascadebooleanDrops also dependent objects

NOTE

ifExists only guards the constraint name (DROP CONSTRAINT IF EXISTS). ifTableExists guards the table (ALTER TABLE IF EXISTS) and can hide typos, schema mistakes, or migration-order issues. It is mainly useful for retry-safe non-transactional migrations.

Operation: renameConstraint

pgm.renameConstraint( tablename, old_constraint_name, new_constraint_name )

IMPORTANT

Rename a constraint - postgres docs

Reverse Operation: same operation in opposite direction

Arguments

NameTypeDescription
tablenameNameName of the table to alter
old_constraint_namestringCurrent constraint name
new_constraint_namestringNew constraint name