# Class: M2MRelation

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 one product and a product may related to more than one invoice. Those relationships are solved using a 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 keys, 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: 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 keys. 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.#### 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)

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

# Hierarchy

# Properties

# foreignKey

foreignKey: ForeignKey

Overrides void

Defined in pg-structure/relation/m2m-relation.ts:120

Foreign key between source table and join table.

# Example

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

# targetForeignKey

targetForeignKey: ForeignKey

Defined in pg-structure/relation/m2m-relation.ts:130

Foreign key between join table and target table.

# Example

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

# Accessors

# info

get info(): string

Overrides void

Defined in pg-structure/relation/m2m-relation.ts:74

Informational text representation of the relation.

# Example

const info = relation.info; // [public.product]――― line_item_product ――⥷ [public.line_item] ⭃―― line_item_cart ―――[public.cart]

Returns: string


# joinAdjective

get joinAdjective(): string | undefined

Defined in pg-structure/relation/m2m-relation.ts:243

Join table adjective

Returns: string | undefined


# joinAlias

get joinAlias(): string

Defined in pg-structure/relation/m2m-relation.ts:228

Join table alias

Returns: string


# joinName

get joinName(): string

Defined in pg-structure/relation/m2m-relation.ts:213

Join table name.

Returns: string


# joinTable

get joinTable(): Table

Defined in pg-structure/relation/m2m-relation.ts:97

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

# Example

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

Returns: Table


# name

get name(): string

Inherited from Relation.name

Defined in pg-structure/base/relation.ts:100

Suggested name for relation.

see {@link ../relation-names.md Relation Names}

Returns: string


# sourceAdjective

get sourceAdjective(): string | undefined

Overrides Relation.sourceAdjective

Defined in pg-structure/relation/m2m-relation.ts:238

Source table adjective

Returns: string | undefined


# sourceAlias

get sourceAlias(): string

Defined in pg-structure/relation/m2m-relation.ts:223

Source table alias

Returns: string


# sourceName

get sourceName(): string

Overrides Relation.sourceName

Defined in pg-structure/relation/m2m-relation.ts:208

Source table name

Returns: string


# sourceTable

get sourceTable(): Table

Overrides void

Defined in pg-structure/relation/m2m-relation.ts:85

Table which this relation belongs to.

# Example

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

Returns: Table


# targetAdjective

get targetAdjective(): string | undefined

Overrides Relation.targetAdjective

Defined in pg-structure/relation/m2m-relation.ts:248

Target table adjective

Returns: string | undefined


# targetAlias

get targetAlias(): string

Defined in pg-structure/relation/m2m-relation.ts:233

Target table alias

Returns: string


# targetName

get targetName(): string

Overrides Relation.targetName

Defined in pg-structure/relation/m2m-relation.ts:218

Target table name

Returns: string


# targetTable

get targetTable(): Table

Overrides void

Defined in pg-structure/relation/m2m-relation.ts:108

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

# Example

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

Returns: Table

# Methods

# getJoinAliasWithout

getJoinAliasWithout(without: M2MWithout | M2MWithout[]): string

Defined in pg-structure/relation/m2m-relation.ts:193

Returns join table alias after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

join table alias after given tables' names replaced.


# getJoinNameWithout

getJoinNameWithout(without: M2MWithout | M2MWithout[]): string

Defined in pg-structure/relation/m2m-relation.ts:163

Returns join table name after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

join table name after given tables' names replaced.


# getName

getName(relationNameFunction: RelationNameFunction | BuiltinRelationNameFunction): string

Inherited from Relation.getName

Defined in pg-structure/base/relation.ts:112

Retunrs name for the relation using given naming function.

Parameters:

Name Type Description
relationNameFunction RelationNameFunction | BuiltinRelationNameFunction is custom function or name of the builtin function to generate names with.

Returns: string

name for the relation using naming function.


# getSourceAliasWithout

getSourceAliasWithout(without: M2MWithout | M2MWithout[]): string

Defined in pg-structure/relation/m2m-relation.ts:183

Returns source table alias after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

source table alias after given tables' names replaced.


# getSourceNameWithout

getSourceNameWithout(without: M2MWithout | M2MWithout[]): string

Overrides Relation.getSourceNameWithout

Defined in pg-structure/relation/m2m-relation.ts:153

Returns source table name after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

source table name after given tables' names replaced.


# getTargetAliasWithout

getTargetAliasWithout(without: M2MWithout | M2MWithout[]): string

Defined in pg-structure/relation/m2m-relation.ts:203

Returns target table alias after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

target table alias after given tables' names replaced.


# getTargetNameWithout

getTargetNameWithout(without: M2MWithout | M2MWithout[]): string

Overrides Relation.getTargetNameWithout

Defined in pg-structure/relation/m2m-relation.ts:173

Returns target table name after replacing given tables' names from it.

Parameters:

Name Type Description
without M2MWithout | M2MWithout[] is type or types of tables to exclude names of.

Returns: string

target table name after given tables' names replaced.