> ## Documentation Index
> Fetch the complete documentation index at: https://docs.serial.okos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Component Links

> An in-depth look at the structure and purpose of the `component_links` table in the Serial database

## Introduction to the Component\_Links Table

The component\_links table is designed to establish hierarchical relationships between components in the Serial database. This table plays a crucial role in tracking the connections between parent and child components, associating them with specific processes and datasets.

## Table Structure

The component\_links table is structured to efficiently represent component relationships and their associated data. Here's a detailed breakdown of its columns:

| Column Name        | Data Type                | Constraints             | Description                                              |
| ------------------ | ------------------------ | ----------------------- | -------------------------------------------------------- |
| id                 | uuid                     | primary key             | Unique identifier for each link                          |
| company\_id        | uuid                     | foreign key, not null   | References the company owning the link                   |
| component\_id      | uuid                     | foreign key, not null   | References the parent component                          |
| has\_child\_of\_id | uuid                     | foreign key, not null   | References the child component                           |
| process\_id        | uuid                     | foreign key, not null   | References the process associated with the link          |
| dataset\_id        | uuid                     | nullable                | References the dataset associated with the link, if any  |
| data\_key\_id      | uuid                     | foreign key, nullable   | References the data key associated with the link, if any |
| created\_at        | timestamp with time zone | not null, default now() | Timestamp of link creation                               |
| is\_active         | boolean                  | not null, default true  | Indicates whether the link is currently active           |

## Usage and Functionality

The component\_links table is designed to be flexible and support complex component hierarchies. Here are some key points about its usage:

1. **Hierarchical Component Relationships**: The table allows for the creation of parent-child relationships between components, enabling the representation of complex product structures or assembly hierarchies.

2. **Process and Dataset Association**: Each link is associated with a specific process, and optionally with a dataset, allowing for detailed tracking of component relationships within different manufacturing or assembly processes.

3. **Unique Constraint**: The table enforces a unique constraint on the combination of (component\_id, has\_child\_of\_id, process\_id, dataset\_id), ensuring that no duplicate links are created for the same component relationship and process-dataset combination.

4. **Active Status Tracking**: The `is_active` column allows for soft deletion or deactivation of links without removing the historical data from the database.

## Notes

* The table has a Row Level Security (RLS) policy that allows only ADMIN users to update rows for their respective companies, ensuring data integrity and access control.

* Example of creating a new component link:

  ```typescript
  const { data, error } = await supabase.from("component_links").insert({
    company_id: "cc1ed015-6c6b-426a-a806-19c500a00aba",
    component_id: "6df39f0a-b1dc-4786-abac-916ef2fcab1f",
    has_child_of_id: "6ed78908-4dea-421f-8550-9c781eec38a4",
    process_id: "14358e0a-5f93-429f-81b6-6930a9c4ee77",
    dataset_id: "9fd0a461-52fc-4ea5-b5fe-7e4f6c47509f",
  });
  ```

* The `data_key_id` column allows for optional association with specific data keys, providing additional flexibility in data management.

By leveraging the component\_links table, the Serial application can maintain a comprehensive and flexible representation of component relationships throughout various manufacturing processes, enabling efficient tracking and analysis of product structures and assembly hierarchies.
