# 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

# Readonly foreignKey

foreignKey: ForeignKey

Overrides void

Defined in src/pg-structure/relation/m2m-relation.ts:146

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)

# Readonly targetForeignKey

targetForeignKey: ForeignKey

Defined in src/pg-structure/relation/m2m-relation.ts:156

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 src/pg-structure/relation/m2m-relation.ts:100

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 src/pg-structure/relation/m2m-relation.ts:269

Join table adjective

Returns: string | undefined


# joinAlias

get joinAlias(): string

Defined in src/pg-structure/relation/m2m-relation.ts:254

Join table alias

Returns: string


# joinName

get joinName(): string

Defined in src/pg-structure/relation/m2m-relation.ts:239

Join table name.

Returns: string


# joinTable

get joinTable(): Table

Defined in src/pg-structure/relation/m2m-relation.ts:123

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

Overrides void

Defined in src/pg-structure/relation/m2m-relation.ts:78

Suggested name for relation.

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

Returns: string


# sourceAdjective

get sourceAdjective(): string | undefined

Overrides Relation.sourceAdjective

Defined in src/pg-structure/relation/m2m-relation.ts:264

Source table adjective

Returns: string | undefined


# sourceAlias

get sourceAlias(): string

Defined in src/pg-structure/relation/m2m-relation.ts:249

Source table alias

Returns: string


# sourceName

get sourceName(): string

Overrides Relation.sourceName

Defined in src/pg-structure/relation/m2m-relation.ts:234

Source table name

Returns: string


# sourceTable

get sourceTable(): Table

Overrides void

Defined in src/pg-structure/relation/m2m-relation.ts:111

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 src/pg-structure/relation/m2m-relation.ts:274

Target table adjective

Returns: string | undefined


# targetAlias

get targetAlias(): string

Defined in src/pg-structure/relation/m2m-relation.ts:259

Target table alias

Returns: string


# targetName

get targetName(): string

Overrides Relation.targetName

Defined in src/pg-structure/relation/m2m-relation.ts:244

Target table name

Returns: string


# targetTable

get targetTable(): Table

Overrides void

Defined in src/pg-structure/relation/m2m-relation.ts:134

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 src/pg-structure/relation/m2m-relation.ts:219

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 src/pg-structure/relation/m2m-relation.ts:189

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(relationNameFunctions: RelationNameFunctions | BuiltinRelationNameFunctions): string

Defined in src/pg-structure/relation/m2m-relation.ts:90

Retunrs name for the relation using given naming function.

Parameters:

Name Type Description
relationNameFunctions RelationNameFunctions | BuiltinRelationNameFunctions are custom functions or name of the builtin functions to generate names with.

Returns: string

name for the relation using naming function.


# getSourceAliasWithout

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

Defined in src/pg-structure/relation/m2m-relation.ts:209

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 src/pg-structure/relation/m2m-relation.ts:179

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 src/pg-structure/relation/m2m-relation.ts:229

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 src/pg-structure/relation/m2m-relation.ts:199

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.