Skip to content

データベーススキーマ / Database Schema

千葉経済産業新聞CRMシステムのデータベース構造を図示したドキュメントです。

全体構造図(ER図)

mermaid
erDiagram
    %% マスターデータ(基幹データ)
    sponsors {
        uuid id PK
        text name
        text plan
        integer monthly_fee
        date start_date
        text status
    }

    leads {
        uuid id PK
        text name
        text org
        text source
        text stage
        uuid owner_id FK
    }

    readers {
        uuid id PK
        text name
        text org
        text subscription
        integer units_per_issue
        text status
    }

    users {
        uuid id PK
        text name
        text email
        text role
    }

    tags {
        uuid id PK
        text name
        text category
    }

    %% トランザクションデータ(活動記録)
    interactions {
        uuid id PK
        uuid sponsor_id FK
        uuid lead_id FK
        uuid reader_id FK
        uuid user_id FK
        text type
        text notes
        timestamp date
    }

    interviews {
        uuid id PK
        uuid sponsor_id FK
        uuid interviewer_id FK
        uuid writer_id FK
        date interview_date
        text status
    }

    invoices {
        uuid id PK
        uuid sponsor_id FK
        uuid reader_id FK
        integer amount
        date due_date
        text status
    }

    finance_transactions {
        uuid id PK
        uuid sponsor_id FK
        text type
        integer amount
        date transaction_date
    }

    %% 多対多中間テーブル
    sponsor_tags {
        uuid sponsor_id FK
        uuid tag_id FK
    }

    lead_tags {
        uuid lead_id FK
        uuid tag_id FK
    }

    reader_tags {
        uuid reader_id FK
        uuid tag_id FK
    }

    %% 関係性(トランザクション → マスター)
    interactions ||--o{ sponsors : "対象企業"
    interactions ||--o{ leads : "対象リード"
    interactions ||--o{ readers : "対象購読者"
    interactions }o--|| users : "担当者"

    interviews }o--|| sponsors : "取材先"
    interviews }o--|| users : "インタビュアー"
    interviews }o--|| users : "執筆者"

    invoices ||--o{ sponsors : "協賛請求"
    invoices ||--o{ readers : "購読請求"

    finance_transactions ||--o{ sponsors : "収益源"

    %% タグ関連
    sponsor_tags }o--|| sponsors : ""
    sponsor_tags }o--|| tags : ""
    lead_tags }o--|| leads : ""
    lead_tags }o--|| tags : ""
    reader_tags }o--|| readers : ""
    reader_tags }o--|| tags : ""

    %% その他の関係
    leads }o--|| users : "担当営業"

データ分類

マスターデータ(基幹データ)

顧客・関係者の基本情報を管理する核となるテーブル群。

テーブル役割主要項目
sponsors協賛企業マスタプラン、月額料金、契約期間、ステータス
leadsリード(見込み客)マスタ獲得経路、営業ステージ、担当者
readers購読者マスタ購読タイプ、部数、支払いサイクル
usersシステムユーザーマスタ編集部、営業、ライター等の担当者
tagsタグマスタ業種、地域、トピック等の分類

トランザクションデータ(活動記録)

日々のビジネス活動や取引を記録するテーブル群。

テーブル役割参照先マスタ
interactions接点履歴sponsors / leads / readers, users
interviews取材記録sponsors, users(担当者)
invoices請求書sponsors / readers
finance_transactions財務トランザクションsponsors(収益の場合)

中間テーブル(多対多関連)

テーブル役割
sponsor_tags協賛企業とタグの紐付け
lead_tagsリードとタグの紐付け
reader_tags購読者とタグの紐付け

データフロー概念図

[営業活動]

  leads (見込み客)
    ↓ 契約
  sponsors (協賛企業)

  ├─ interactions (営業・編集の接点記録)
  ├─ interviews (定期取材実施)
  ├─ invoices (月次請求)
  └─ finance_transactions (入金記録)

  [継続・解約判定]


[購読活動]

  readers (購読者)

  ├─ interactions (お問い合わせ対応等)
  └─ invoices (購読料請求)

  finance_transactions (入金記録)

主要な設計方針

  1. ポリモーフィック関連interactions テーブルは、sponsor_id / lead_id / reader_id の3つの外部キーを持ち、どの対象タイプとの接点かを柔軟に記録できる設計。

  2. マスター中心設計 すべてのトランザクションは必ずマスターデータ(sponsors/leads/readers/users)を参照し、データの整合性を保つ。

  3. タグによる柔軟な分類 多対多の中間テーブルを用いて、業種・地域・トピック等を自由に紐付け可能。

  4. 監査証跡 各テーブルに created_at / updated_at を持ち、変更履歴を追跡可能。

関連ドキュメント

  • 実装詳細: /supabase/migrations/001_initial_schema.sql
  • 型定義: /app/src/types/database.types.ts
  • 運用方針: /CLAUDE.md

千葉経済産業新聞