Constraint

Class which represent a constraint. Provides attributes and methods for details of the constraint.

Notes for Through Constraints

Through constraints are used for many to many relationships. Actually there isn't such a thing called many to many relationship or through constraint in the database engine. They are concepts to describe records which may be related more than one record on both sides. For example an invoice may contain more than product and a product may related to more than one invoice. Those relationships are solved a so called many to many join table.

Constraint class supports many to many relationships. Since those constraints are not present in database engine, they are extracted by estimation/interpretation. Many non-join tables in a database could have more than one foreign key constraints, and they may not meant to be join tables, but they have still through relationships .

Below is a database schema as an example:

size -------------------
id (PK)                |  ---------------------------< line_item >------------ cart
name                   |  |                            product_id (PFK)        id (PK)
                       |  |                            cart_id    (PFK)        name
                       ^  |
color -------------< product >------------- vendor
id (PK)              id        (PK)         id (PK)
name                 name                   name
                     color_id  (FK)
                     size_id   (FK)
                     vendor_id (FK)

Below is the same schema as image: Database Schema

Product table has 3 foreign key constraints. It is obvious that product table is not meant to be a many to many join table. However product could have been join table for size & vendor, color & vendor and size & color. As a result size, color and vendor tables would have many to many through constraints.

Kind: global class

new Constraint(args)

Constructor function. You don't need to call constructor manually. pg-structure handles this.

Param Type Description
args Object Attributes of the Constraint instance to be created.
args.parent Table For foreign key constraints this is Table instance this constraint refers to. If this is not a foreign key constraint this is null.
args.table Table Table instance the Constraint is defined in.
args.schemaName string Schema name of the Constraint.
args.name string Name of the Constraint.
args.type string Constraint type.
args.description string Description of the constraint.
args.descriptionData Object Extra data to store in object.
args.onUpdate string Update rule of the constraint.
args.onDelete string Delete rule of the constraint.
args.matchOption string Match option of the constraint.

constraint.name : string

Name of the constraint.

Kind: instance property of Constraint
Read only: true

constraint.fullName : string

Full name of the constraint with (.) notation.

Kind: instance property of Constraint
Read only: true
Example

var fullName = constraint.fullName; // crm.public

constraint.fullCatalogName : string

Full name of the constraint with (.) notation including catalog name.

Kind: instance property of Constraint
Read only: true
Example

var fullCatalogName = constraint.fullCatalogName; // crm.public

constraint.type : contsraintType

Constraint type. One of PRIMARY KEY, FOREIGN KEY or CHECK

Kind: instance property of Constraint
Read only: true

constraint.comment : string

Comment of the Constraint.

Kind: instance property of Constraint
Read only: true
See: Aliases description

constraint.commentData : Object

JS Object extracted from constraint description. Object is expected as JSON data between [JSON] and [/JSON] tags in description. Tags are case-insensitive.

Kind: instance property of Constraint
Read only: true
See: Aliases descriptionData
Example

let description = constraint.comment;            // -> 'This contraint refers contacts. [JSON]{ "extraData": 2 }[/PGEN]'
let extra = constraint.commentData;              // -> { extraData: 2 }
console.log(constraint.commentData.extraData);   // -> 2

constraint.description : string

Comment of the Constraint.

Kind: instance property of Constraint
Read only: true
See: Aliases comment

constraint.descriptionData : Object

JS Object extracted from constraint description. Object is expected as JSON data between [JSON] and [/JSON] tags in description. Tags are case-insensitive.

Kind: instance property of Constraint
Read only: true
See: Aliases commentData
Example

let description = constraint.description;            // -> 'This contraint refers contacts. [JSON]{ "extraData": 2 }[/PGEN]'
let extra = constraint.descriptionData;              // -> { extraData: 2 }
console.log(constraint.descriptionData.extraData);   // -> 2

constraint.child : Table

Child table of this constraint. Note for foreign key constraints: Child table is the table which contains foreign key. In example schema product is a child table (vendor_id FK) of vendor table.

Kind: instance property of Constraint
Read only: true
Example

var table = constraint.child;

constraint.table : Table

Table which this constraint belongs to or defined in.
Note for foreign key constraints: As usual PostgreSQL defines foreign key constraints in child tables, where foreign key column is defined, so this is child table for foreign key constraints.

Kind: instance property of Constraint
Read only: true
Example

var table = constraint.table;

constraint.db : Db

Db this constraint belongs to.

Kind: instance property of Constraint
Read only: true

constraint.schema : Schema

Schema this constraint belongs to.

Kind: instance property of Constraint
Read only: true

constraint.matchOption : string

Match option of Constraint.

Kind: instance property of Constraint
Read only: true

constraint.onUpdate : constraintRule | null

Update rule for foreign key constraints. One of CASCADE, SET NULL, SET DEFAULT, RESTRICT, NO ACTION If this is not a foreign key constraint this is null.

Kind: instance property of Constraint
Read only: true

constraint.onDelete : constraintRule | null

Update rule for foreign key constraints. One of CASCADE, SET NULL, SET DEFAULT, RESTRICT, NO ACTION If this is not a foreign key constraint this is null.

Kind: instance property of Constraint
Read only: true

constraint.referencedTable : Table | null

For foreign key constraints this is Table instance this constraint refers to. If this is not a foreign key constraint this is null.

Kind: instance property of Constraint
Read only: true
See: Aliases parent

constraint.parent : Table | null

For foreign key constraints this is Table instance this constraint refers to. If this is not a foreign key constraint this is null.
Please Note: This is not the Table this constraint belongs to or defined in. Parent applies only to foreign key constraints and for foreign key constraints parent means referenced table not the table it is defined in.

Kind: instance property of Constraint
Read only: true
See

constraint.columns : Map.<Column>

For foreign key constraints, this is map of columns restricted by constraint, in order their ordinal position within the constraint key.

Kind: instance property of Constraint
Read only: true

constraint.referencedColumnsBy : Map.<Column>

For foreign key constraints, this is map of columns referenced by this constraint's columns. Keys are referencing column's names, values are referenced columns.

Kind: instance property of Constraint
Read only: true