> ## 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.

# Station Process Links

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

## Introduction to the Station\_Process\_Links Table

The station\_process\_links table is designed to establish relationships between stations, processes, and components in the Serial application. This table plays a crucial role in managing the complex associations between these entities, enabling efficient querying and manipulation of production data.

## Table Structure

The station\_process\_links table is structured to create many-to-many relationships between stations, processes, and components. Here's a detailed breakdown of its columns:

| Column Name       | Data Type | Constraints                    | Description                                          |
| ----------------- | --------- | ------------------------------ | ---------------------------------------------------- |
| id                | uuid      | primary key, not null, default | Unique identifier for each link                      |
| company\_id       | uuid      | not null, foreign key          | References the company associated with the link      |
| station\_id       | uuid      | not null, foreign key          | References the station involved in the link          |
| component\_id     | uuid      | not null, foreign key          | References the component involved in the link        |
| process\_id       | uuid      | not null, foreign key          | References the process involved in the link          |
| process\_revision | int8      | added later                    | Stores the revision number of the associated process |

## Usage and Functionality

The station\_process\_links table is designed to be a central point for managing relationships between stations, processes, and components. Here are some key points about its usage:

1. **Relationship Management**: It allows for flexible association of processes and components with specific stations, enabling complex production workflows.

2. **Querying Station Details**: The table is crucial when fetching station information along with its associated processes and components. This is evident in functions like `stations-page` which retrieves detailed station data including linked processes.

3. **Production Validation**: In the production app, this table is used to validate which processes are associated with a station, ensuring that only valid operations are performed.

## Notes

* Row-level security is enabled on the station\_process\_links table, ensuring data access is properly controlled.

* There are specific policies for inserting, selecting, and deleting records based on user roles and company associations. For example:

  ```sql
  CREATE POLICY "Enable delete for users based on user_id" ON "public"."station_process_links"
  AS PERMISSIVE FOR DELETE
  TO public
  USING (EXISTS ( SELECT 1
     FROM privileges p
    WHERE ((p.company_id = station_process_links.company_id) AND (p.user_id = auth.uid()) AND (p.role = ANY (ARRAY['ADMIN'::text, 'ENGINEER'::text, 'TECHNICIAN'::text])))))
  ```

* The table is involved in operations like linking all processes to a station or deleting specific station-process links, as seen in functions like `linkAllProcesses` and `deleteStationProcessLink`.

By leveraging the station\_process\_links table, the Serial application can efficiently manage and query the complex relationships between stations, processes, and components, providing a flexible and powerful foundation for production management and tracking.
