M2MRelation

Kind: global class

new M2MRelation(args)

Class which represent a many to many relationship which resembles belongsToMany or hasManyThrough relations in ORMs (Object Relational Mappers). Provides attributes and methods for details of the relationship.

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.

Since those relations 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 still appear to have 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

Some definitions used in descriptions for M2MRelation. Source Table: Table which this relationship belongs to. Join Table: Table that contains common fields from two or more other tables. * Target Table: Table that is related to base table through a join table.

Product table has 3 foreign key constraints. 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 relationships.

Param Type Description
args Object Attributes of the M2MRelation instance to be created.
args.sourceTable Table Source Table which this relation belongs to.
args.joinTable Table Join Table of this relationship.
args.targetTable Table Target Table which this relation is referring to through a join table.
args.sourceConstraint Constraint Foreign key constraint between source table and join table.
args.targetConstraint Constraint Foreign key constraint between join table and target table.

Example

// Example tables have single primary key and and examples first relation. So zero index ([0]) is used. Use all array elements if necessary.
// product ----< line_item >---- cart
// (source)        (join)       (target)

let relation             = product.m2mRelations[0];              // RELATION:    product ---< line_item >--- cart
let sourceConstraint     = relation.sourceConstraint;            // CONSTRAINT:           ^-- product_has_carts
let targetConstraint     = relation.targetConstraint;            // CONSTRAINT:       cart_has_products --^
let sourceTable          = relation.sourceTable;                 // TABLE:       product
let targetTable          = relation.targetTable;                 // TABLE:       cart
let sourceJoinFKColumn   = relation.sourceConstraint.columns[0]; // COLUMN:      product_id  (from line_item table)
let targetJoinFKColumn   = relation.targetConstraint.columns[0]; // COLUMN:      cart_id     (from line_item table)
let sourcePKColumn       = relation.sourceTable.primaryKeys[0];  // COLUMN:      id          (from product table)
let targetPKColumn       = relation.targetTable.primaryKeys[0];  // COLUMN:      id          (from cart table)

m2MRelation.type : relationType

Type of relation which is MANY TO MANY.

Kind: instance property of M2MRelation
Read only: true

m2MRelation.sourceTable : Table

Table which this relation belongs to.

Kind: instance property of M2MRelation
Read only: true
Example

let relation = product.M2MRelationRelations[0];  // RELATION:    product ---< line_item >--- cart
let source   = relation.sourceTable;             // TABLE:       product

m2MRelation.joinTable : Table

Join Table of this relationship. This table contains foreign key columns referring both sourceTable and targetTable.

Kind: instance property of M2MRelation
Read only: true
Example

let relation  = product.M2MRelationRelations[0]; // RELATION:    product ---< line_item >--- cart
let joinTable = relation.joinTable;              // TABLE:       line_item

m2MRelation.targetTable : Table

Table which this relation is referring to (Through a join table).

Kind: instance property of M2MRelation
Read only: true
Example

let relation = product.M2MRelationRelations[0];  // RELATION:    product ---< line_item >--- cart
let target   = relation.targetTable;             // TABLE:       cart

m2MRelation.sourceConstraint : Table

Foreign key constraint between source table and join table.

Kind: instance property of M2MRelation
Read only: true
Example

let relation             = product.M2MRelationRelations[0];      // RELATION:    product ---< line_item >--- cart
let sourceConstraint     = relation.sourceConstraint;            // CONSTRAINT:           ^-- product_has_carts
let sourceJoinFKColumn   = relation.sourceConstraint.columns[0]; // COLUMN:      product_id (from line_item table)

m2MRelation.targetConstraint : Table

Foreign key constraint between join table and target table.

Kind: instance property of M2MRelation
Read only: true
Example

let relation             = product.M2MRelationRelations[0];      // RELATION:    product ---< line_item >--- cart
let targetConstraint     = relation.targetConstraint;            // CONSTRAINT:       cart_has_products --^
let targetJoinFKColumn   = relation.targetConstraint.columns[0]; // COLUMN:      cart_id (from line_item table)

m2MRelation.generateName([strategy]) ⇒ string

(! EXPERIMENTAL) Returns name for relation using given strategy. Please see Relation Names for details.

Kind: instance method of M2MRelation
Returns: string - - Relation name.
See: Relation Names

Param Type Description
[strategy] string (simple, complex) Naming strategy to use.