Skip to content

Column Operations

Column Definitions

The createTable and addColumns methods both take a columns argument that specifies column names and options. It is an object (key/value) where each key is the name of the column, and the value is another object that defines the options for the column.

OptionTypeDescription
typestringData type (use normal postgres types)
collationstringCollation of data type
uniquebooleanSet to true to add a unique constraint on this column
primaryKeybooleanSet to true to make this column the primary key
notNullbooleanSet to true to make this column not null
defaultstringAdds DEFAULT clause for column. Accepts null, a literal value, or a pgm.func() expression.
checkstringSQL for a check constraint for this column
referencesName or stringA table name that this column is a foreign key to
referencesConstraintNamestringName of the created constraint
referencesConstraintCommentstringComment on the created constraint
onDeletestringAdds ON DELETE constraint for a reference column
onUpdatestringAdds ON UPDATE constraint for a reference column
matchstringFULL or SIMPLE
deferrablebooleanFlag for deferrable column constraint
deferredbooleanFlag for initially deferred deferrable column constraint
commentstringAdds comment on column
expressionGeneratedstringExpression to compute column value
sequenceGeneratedobjectCreates identity column see sequence options section
precedencestringALWAYS or BY DEFAULT

Data types & Convenience Shorthand

Data type strings will be passed through directly to postgres, so write types as you would if you were writing the queries by hand.

There are some aliases on types to make things more foolproof: (int, string, float, double, datetime, bool)

There is a shorthand to pass only the type instead of an option object:

ts
pgm.addColumns('myTable', { age: 'integer' });

Equivalent to:

ts
pgm.addColumns('myTable', { age: { type: 'integer' } });

There is a shorthand for normal auto-increment IDs:

ts
pgm.addColumns('myTable', { id: 'id' });

Equivalent to:

ts
pgm.addColumns('myTable', { id: { type: 'serial', primaryKey: true } });

Methods

Operation: addColumns

pgm.addColumns( tablename, new_columns, options )

IMPORTANT

Add columns to an existing table - postgres docs

Alias: addColumn

Arguments

NameTypeDescription
tablenameNameName of the table to alter
new_columnsobjectColumn names / options -- see column definitions section
optionsobjectCheck below for available options
Options
OptionTypeDescription
ifNotExistsbooleanAdds column only if it does not exist

Reverse Operation: dropColumns

pgm.dropColumns( tablename, columns, options )

IMPORTANT

Drop columns from a table - postgres docs

Alias: dropColumn

Arguments

NameTypeDescription
tablenameNameName of the table to alter
columnsarray of strings or objectColumns to drop (if objected, uses keys)
optionsobjectCheck below for available options
Options
OptionTypeDescription
ifExistsbooleanDrops column only if it exists
cascadebooleanDrop also dependent objects

Operation: renameColumn

pgm.renameColumn( tablename, old_column_name, new_column_name )

IMPORTANT

Rename a column - postgres docs

Reverse Operation: same operation in opposite direction

Arguments

NameTypeDescription
tablenameNameName of the table
old_column_namestringCurrent column name
new_column_namestringNew column name

Operation: alterColumn

pgm.alterColumn( tablename, column_name, column_options )

IMPORTANT

Alter a column (default value, type, allow null) - postgres docs

Arguments

NameTypeDescription
tablenameNameName of the table
column_namestringColumn to alter
column_optionsobjectOptional new column options
Column Options
OptionTypeDescription
defaultstring or nullnull, string
typestringNew datatype
notNullbooleanSets NOT NULL if true or NULL if false
allowNullbooleanSets NULL if true (alternative to notNull)
usingstringAdds USING clause to change values in column
collationstringAdds COLLATE clause to change values in column
commentstringAdds comment on column
sequenceGeneratedobject or null or falseSets or drops identity column
sequenceGenerated Options

See sequence options section, You can also set precedence to ALWAYS or BY DEFAULT.