# ER図

## 概要

本ドキュメントは、InvenTreeプロジェクトのデータベースエンティティ間の関係をMermaid形式で記述したER図です。

## 全体ER図

```mermaid
erDiagram
    %% Part Management
    PartCategory ||--o{ PartCategory : "parent"
    PartCategory ||--o{ Part : "contains"
    PartCategory ||--o{ PartCategoryParameterTemplate : "has"

    Part ||--o{ Part : "variant_of"
    Part ||--o{ Part : "revision_of"
    Part ||--o{ BomItem : "parent"
    Part ||--o{ BomItem : "sub_part"
    Part ||--o{ StockItem : "instances"
    Part ||--o{ SupplierPart : "supplier_parts"
    Part ||--o{ ManufacturerPart : "manufacturer_parts"
    Part ||--o{ Build : "builds"
    Part ||--o{ SalesOrderLineItem : "sales_lines"

    %% Stock Management
    StockLocationType ||--o{ StockLocation : "type"
    StockLocation ||--o{ StockLocation : "parent"
    StockLocation ||--o{ StockItem : "contains"

    StockItem ||--o{ StockItem : "parent"
    StockItem ||--o{ StockItem : "belongs_to"
    StockItem ||--o{ StockItemTracking : "tracking"
    StockItem ||--o{ StockItemTestResult : "test_results"
    StockItem ||--o{ SalesOrderAllocation : "allocations"
    StockItem ||--o{ BuildItem : "build_allocations"

    %% Company Management
    Company ||--o{ Contact : "contacts"
    Company ||--o{ Address : "addresses"
    Company ||--o{ SupplierPart : "supplied_parts"
    Company ||--o{ ManufacturerPart : "manufactured_parts"
    Company ||--o{ PurchaseOrder : "purchase_orders"
    Company ||--o{ SalesOrder : "sales_orders"
    Company ||--o{ ReturnOrder : "return_orders"

    ManufacturerPart ||--o{ SupplierPart : "supplier_parts"

    SupplierPart ||--o{ SupplierPriceBreak : "price_breaks"
    SupplierPart ||--o{ PurchaseOrderLineItem : "po_lines"
    SupplierPart ||--o{ StockItem : "stock_items"

    %% Purchase Order
    PurchaseOrder ||--o{ PurchaseOrderLineItem : "lines"
    PurchaseOrder ||--o{ PurchaseOrderExtraLine : "extra_lines"
    PurchaseOrder ||--o{ StockItem : "received_items"

    %% Sales Order
    SalesOrder ||--o{ SalesOrderLineItem : "lines"
    SalesOrder ||--o{ SalesOrderExtraLine : "extra_lines"
    SalesOrder ||--o{ SalesOrderShipment : "shipments"

    SalesOrderLineItem ||--o{ SalesOrderAllocation : "allocations"
    SalesOrderShipment ||--o{ SalesOrderAllocation : "allocations"

    %% Return Order
    ReturnOrder ||--o{ ReturnOrderLineItem : "lines"
    ReturnOrder ||--o{ ReturnOrderExtraLine : "extra_lines"

    %% Build Order
    Build ||--o{ Build : "parent"
    Build ||--o{ BuildLine : "build_lines"
    Build ||--o{ StockItem : "build_outputs"
    Build ||--o{ StockItem : "consumed_items"

    BuildLine ||--o{ BuildItem : "allocations"

    %% User and Owner
    Owner ||--o{ Part : "responsible_parts"
    Owner ||--o{ StockLocation : "owned_locations"
    Owner ||--o{ PurchaseOrder : "responsible_po"
    Owner ||--o{ SalesOrder : "responsible_so"
    Owner ||--o{ Build : "responsible_builds"
    Owner ||--o{ ProjectCode : "project_codes"

    %% Project Code
    ProjectCode ||--o{ PurchaseOrder : "purchase_orders"
    ProjectCode ||--o{ SalesOrder : "sales_orders"
    ProjectCode ||--o{ Build : "builds"
    ProjectCode ||--o{ ReturnOrder : "return_orders"
```

## 部品管理ER図（詳細）

```mermaid
erDiagram
    PartCategory {
        int id PK
        string name
        string description
        int parent_id FK
        int default_location_id FK
        string default_keywords
        boolean structural
        string icon
        jsonb metadata
    }

    Part {
        int id PK
        string name
        string description
        string keywords
        string IPN
        string revision
        int revision_of_id FK
        int category_id FK
        int variant_of_id FK
        int default_location_id FK
        int default_supplier_id FK
        int default_expiry
        decimal minimum_stock
        string units
        boolean is_template
        boolean assembly
        boolean component
        boolean trackable
        boolean testable
        boolean purchaseable
        boolean salable
        boolean active
        boolean locked
        boolean virtual
        string link
        string image
        boolean bom_validated
        string bom_checksum
        int bom_checked_by_id FK
        date bom_checked_date
        date creation_date
        int creation_user_id FK
        int responsible_owner_id FK
        jsonb metadata
    }

    BomItem {
        int id PK
        int part_id FK
        int sub_part_id FK
        decimal quantity
        string reference
        string note
        boolean optional
        boolean consumable
        boolean allow_variants
        boolean inherited
        boolean validated
        jsonb metadata
    }

    PartCategoryParameterTemplate {
        int id PK
        int category_id FK
        int template_id FK
        string default_value
        jsonb metadata
    }

    PartCategory ||--o{ PartCategory : "parent"
    PartCategory ||--o{ Part : "category"
    PartCategory ||--o{ PartCategoryParameterTemplate : "templates"
    Part ||--o{ Part : "variant_of"
    Part ||--o{ Part : "revision_of"
    Part ||--o{ BomItem : "part"
    Part ||--o{ BomItem : "sub_part"
```

## 在庫管理ER図（詳細）

```mermaid
erDiagram
    StockLocationType {
        int id PK
        string name
        string description
        string icon
        jsonb metadata
    }

    StockLocation {
        int id PK
        string name
        string description
        int parent_id FK
        int location_type_id FK
        int owner_id FK
        boolean structural
        boolean external
        string icon
        string pathstring
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    StockItem {
        int id PK
        int part_id FK
        int supplier_part_id FK
        int location_id FK
        int parent_id FK
        int belongs_to_id FK
        int customer_id FK
        decimal quantity
        string serial
        int serial_int
        string batch
        string link
        string packaging
        int status
        date expiry_date
        date stocktake_date
        int stocktake_user_id FK
        boolean review_needed
        boolean delete_on_deplete
        int build_id FK
        boolean is_building
        int purchase_order_id FK
        int sales_order_id FK
        int consumed_by_id FK
        decimal purchase_price
        string purchase_price_currency
        string barcode_data
        string barcode_hash
        datetime updated
        jsonb metadata
    }

    StockItemTracking {
        int id PK
        int item_id FK
        int tracking_type
        datetime date
        int user_id FK
        string notes
        jsonb deltas
    }

    StockItemTestResult {
        int id PK
        int stock_item_id FK
        int template_id FK
        boolean result
        string value
        string notes
        datetime date
        int user_id FK
    }

    StockLocationType ||--o{ StockLocation : "type"
    StockLocation ||--o{ StockLocation : "parent"
    StockLocation ||--o{ StockItem : "location"
    StockItem ||--o{ StockItem : "parent"
    StockItem ||--o{ StockItem : "belongs_to"
    StockItem ||--o{ StockItemTracking : "tracking"
    StockItem ||--o{ StockItemTestResult : "test_results"
```

## 発注管理ER図（詳細）

```mermaid
erDiagram
    Company {
        int id PK
        string name
        string description
        string website
        string phone
        string email
        boolean active
        boolean is_customer
        boolean is_supplier
        boolean is_manufacturer
        string currency
        string tax_id
        jsonb metadata
    }

    ManufacturerPart {
        int id PK
        int part_id FK
        int manufacturer_id FK
        string MPN
        string link
        string description
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    SupplierPart {
        int id PK
        int part_id FK
        int supplier_id FK
        int manufacturer_part_id FK
        string SKU
        boolean active
        string link
        string description
        string note
        decimal base_cost
        string packaging
        string pack_quantity
        int multiple
        decimal available
        datetime availability_updated
        string barcode_data
        string barcode_hash
        datetime updated
        jsonb metadata
    }

    SupplierPriceBreak {
        int id PK
        int part_id FK
        decimal quantity
        decimal price
        string price_currency
        datetime updated
    }

    PurchaseOrder {
        int id PK
        string reference
        int supplier_id FK
        string supplier_reference
        string description
        int status
        string link
        int project_code_id FK
        date creation_date
        int created_by_id FK
        date issue_date
        date start_date
        date target_date
        date complete_date
        int received_by_id FK
        int responsible_id FK
        int contact_id FK
        int address_id FK
        int destination_id FK
        decimal total_price
        string order_currency
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    PurchaseOrderLineItem {
        int id PK
        int order_id FK
        int part_id FK
        decimal quantity
        decimal received
        string reference
        string notes
        string link
        date target_date
        int project_code_id FK
        decimal purchase_price
        int destination_id FK
        int build_order_id FK
        jsonb metadata
    }

    Company ||--o{ ManufacturerPart : "manufacturer"
    Company ||--o{ SupplierPart : "supplier"
    Company ||--o{ PurchaseOrder : "supplier"
    ManufacturerPart ||--o{ SupplierPart : "manufacturer_part"
    SupplierPart ||--o{ SupplierPriceBreak : "price_breaks"
    SupplierPart ||--o{ PurchaseOrderLineItem : "po_lines"
    PurchaseOrder ||--o{ PurchaseOrderLineItem : "lines"
```

## 受注管理ER図（詳細）

```mermaid
erDiagram
    SalesOrder {
        int id PK
        string reference
        int customer_id FK
        string customer_reference
        string description
        int status
        string link
        int project_code_id FK
        date creation_date
        int created_by_id FK
        date issue_date
        date start_date
        date target_date
        date shipment_date
        int shipped_by_id FK
        int responsible_id FK
        int contact_id FK
        int address_id FK
        decimal total_price
        string order_currency
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    SalesOrderLineItem {
        int id PK
        int order_id FK
        int part_id FK
        decimal quantity
        decimal shipped
        string reference
        string notes
        string link
        date target_date
        int project_code_id FK
        decimal sale_price
        jsonb metadata
    }

    SalesOrderShipment {
        int id PK
        int order_id FK
        string reference
        date shipment_date
        date delivery_date
        string tracking_number
        int shipment_address_id FK
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    SalesOrderAllocation {
        int id PK
        int line_id FK
        int item_id FK
        decimal quantity
        int shipment_id FK
    }

    SalesOrder ||--o{ SalesOrderLineItem : "lines"
    SalesOrder ||--o{ SalesOrderShipment : "shipments"
    SalesOrderLineItem ||--o{ SalesOrderAllocation : "allocations"
    SalesOrderShipment ||--o{ SalesOrderAllocation : "shipment_allocations"
```

## 製造管理ER図（詳細）

```mermaid
erDiagram
    Build {
        int id PK
        string reference
        string title
        int part_id FK
        int quantity
        int completed
        int status
        string batch
        boolean external
        int parent_id FK
        int sales_order_id FK
        int take_from_id FK
        int destination_id FK
        int project_code_id FK
        string link
        date creation_date
        date start_date
        date target_date
        date completion_date
        int completed_by_id FK
        int issued_by_id FK
        int responsible_id FK
        int priority
        string barcode_data
        string barcode_hash
        jsonb metadata
    }

    BuildLine {
        int id PK
        int build_id FK
        int bom_item_id FK
        decimal quantity
    }

    BuildItem {
        int id PK
        int build_line_id FK
        int stock_item_id FK
        decimal quantity
        int install_into_id FK
    }

    Build ||--o{ Build : "parent"
    Build ||--o{ BuildLine : "build_lines"
    BuildLine ||--o{ BuildItem : "allocations"
```

## 関係性サマリー

### 主要な関係性

| 関係 | カーディナリティ | 説明 |
| --- | --- | --- |
| PartCategory - Part | 1:N | カテゴリは複数の部品を含む |
| Part - StockItem | 1:N | 部品は複数の在庫品目を持つ |
| Part - BomItem | 1:N | 部品は複数のBOM項目を持つ（親として） |
| Part - BomItem | 1:N | 部品は複数のBOM項目で使用される（構成部品として） |
| Company - SupplierPart | 1:N | 仕入先は複数の仕入先部品を持つ |
| Company - PurchaseOrder | 1:N | 仕入先は複数の発注書を持つ |
| Company - SalesOrder | 1:N | 顧客は複数の受注書を持つ |
| PurchaseOrder - PurchaseOrderLineItem | 1:N | 発注書は複数の明細を持つ |
| SalesOrder - SalesOrderLineItem | 1:N | 受注書は複数の明細を持つ |
| SalesOrder - SalesOrderShipment | 1:N | 受注書は複数の出荷を持つ |
| Build - BuildLine | 1:N | 製造指示は複数の構成部品行を持つ |
| StockItem - StockItemTracking | 1:N | 在庫品目は複数の追跡履歴を持つ |

### 自己参照関係

| テーブル | 関係カラム | 説明 |
| --- | --- | --- |
| PartCategory | parent_id | カテゴリの階層構造 |
| Part | variant_of_id | バリアント部品の関係 |
| Part | revision_of_id | リビジョン部品の関係 |
| StockLocation | parent_id | ロケーションの階層構造 |
| StockItem | parent_id | 在庫品目の親子関係 |
| StockItem | belongs_to_id | 在庫品目の所属関係 |
| Build | parent_id | 製造指示の親子関係 |
