Foreign key between source table and join table.
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)
Foreign key between join table and target table.
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)
Whether the relation targets to many. Since, many to many relations targets many, this is true
.
Type of the relation, which is m2m
for M2MRelation.
For TypeScript it is enum of RelationType.M2M
.
Informational text representation of the relation.
const info = relation.info; // [public.product]――― line_item_product ――⥷ [public.line_item] ⭃―― line_item_cart ―――[public.cart]
Join table adjective
Join table alias
Join table name.
Join Table of this relationship. This table contains foreign key columns referring both sourceTable and targetTable.
const relation = product.M2MRelationRelations[0]; // RELATION: product ---< line_item >--- cart
const joinTable = relation.joinTable; // TABLE: line_item
Suggested name for relation.
Source table adjective
Source table alias
Source table name
Target table adjective
Target table alias
Target table name
Returns join table alias after replacing given tables' names from it.
is type or types of tables to exclude names of.
join table alias after given tables' names replaced.
Returns join table name after replacing given tables' names from it.
is type or types of tables to exclude names of.
join table name after given tables' names replaced.
Returns name for the relation using given naming function.
are custom functions or name of the module that exports relation name functions to generate names with. pg-structure
provides some builtin modules (short
, optimal
and descriptive
), but you can use your own.
name for the relation using naming function.
Returns source table alias after replacing given tables' names from it.
is type or types of tables to exclude names of.
source table alias after given tables' names replaced.
Returns source table name after replacing given tables' names from it.
is type or types of tables to exclude names of.
source table name after given tables' names replaced.
Returns target table alias after replacing given tables' names from it.
is type or types of tables to exclude names of.
target table alias after given tables' names replaced.
Returns target table name after replacing given tables' names from it.
is type or types of tables to exclude names of.
target table name after given tables' names replaced.
Generated using TypeDoc
Class which represent a many to many relationship which resembles
belongsToMany
orhasManyThrough
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:
Some definitions used in descriptions for M2MRelation.
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
andsize & color
. As a result size, color and vendor tables would have many to many relationships.Example
```typescript // 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) ```