# Relation Names

Relations are defined using foreign keys. pg-structure provides classes for one to many, many to one and many to many relations.

Whereas foreign keys are defined in DBMS and have names, relations are not present actually in DBMS and therefore they do not have names defined in database system. As a result relation names have to be generated. Naming involves personal preferences, and pg-structure lets developers define their own relation naming functions.

Below is the database schema used in examples:

Database Schema

# Builtin Naming Functions

In addition to custom naming functions, pg-structure provides two built-in function: short and desccriptive. They use foreign key names, join table names and target table names to generate relation names.

# Foreign Key Names

To use builtin functions effectively, foreign keys should be named as one of the below schemes (examples are for product and vendor tables.)

  • Tables: table1_table2 (i.e. product_vendor or vendor_products)
  • Tables & Optional Adjectives: adjective_table1_adjective_table2 (i.e. product_alternative_vendor, alternative_vendor_products or alternative_vendor_primary_products)
  • Separator: source,target or target,source (i.e. item,supplier, supplier,items or item,alternative_supplier )

Both short and descriptive functions are developed based on long experience in PostgreSQL, but they do not guarantee to produce unique names in all possible scenarios. descriptive function has less chance to cause name collisions compared to short.

# Example Results

Below are generated names for example. Differences between short and long are indicated with (•).

Tables Type Short Descriptive
cartline_item O2M line_items line_items
cart → ... → color M2M colors colors
cart → ... → product M2M products products
colorline_item O2M line_items line_items
colorproduct_color O2M product_colors product_colors
color → ... → cart M2M carts carts
color → ... → product M2M line_item_products line_item_products
color → ... → product M2M products color_products
line_itemcart M2O cart cart
line_itemcolor M2O color color
line_itemproduct M2O product product
productline_item O2M line_items line_items
productproduct_color O2M product_colors product_colors
productvendor M2O alternative_vendor alternative_vendor
productvendor M2O vendor vendor
product → ... → cart M2M carts carts
product → ... → color M2M colors product_colors
product → ... → color M2M line_item_colors line_item_colors
product_colorcolor M2O color color
product_colorproduct M2O product product
vendorproduct O2M alternative_products alternative_vendor_products
vendorproduct O2M vendor_products vendor_products
vendor → ... → vendor M2M alternative_vendors alternative_vendors
vendor → ... → vendor M2M vendors vendors

Provided built-in functions short and descriptive tries to generate shortest name possible considering number of relations and number of join tables between same tables. descriptive adds adjectives more freely compared to short. Most of the time, they generate same names.

# Custom Functions

It could be a better solution to provide your own custom naming function according to your needs. Please see relation name functions description for details.